ThinkPHP connection to the database and master/slave database settings

Source: Internet
Author: User
Create config. php: & lt ;? Phpif (! Defined (& amp; #39; THINK_PATH & amp; #39;) exit (); returnarray (& amp; #39; DB_TYPE & amp; #39; & gt; & amp; #39; mysql & amp; #39;, // database class...

Create config. php in the project Root Directory:

  1. If (! Defined ('think _ path') exit ();
  2. Return array (
  3. 'Db _ type' => 'mysql', // Database TYPE
  4. 'Db _ host' => 'localhost', // HOST
  5. 'Db _ name' => 'aoli', // database NAME
  6. 'Db _ user' => 'root', // database username
  7. 'Db _ pwd' => '', // database password
  8. 'Db _ prefix' => '', // data table PREFIX
  9. 'Db _ charset' => 'utf8', // website code
  10. 'Db _ port' => '123', // database PORT
  11. );
  12. ?>

\ Home \ Conf \ config. php

  1. $ Arr1 = array {
  2. 'URL _ model' => 2, // pathinfo access mode
  3. };
  4. $ Arr2 = include './config. php ';
  5. Return array_merge ($ arr1, $ arr2); // array integration
  6. ?>

\ Admin \ Conf \ config. php

  1. $ Arr1 = array {
  2. 'URL _ model' => 1, // get mode in normal access mode
  3. };
  4. $ Arr2 = include './config. php ';
  5. Return array_merge ($ arr1, $ arr2); // array integration
  6. ?>

Master/slave database settings

Suitable for high concurrency and high load. you can view the default system constant settings in \ ThinkPHP \ Common \ convention. php.

Config. php

  1. Return array (
  2. // 'Config maps '=> 'configuration value'
  3. // Background
  4. 'URL _ mode' => 0,
  5. 'Db _ type' => 'mysql ',
  6. 'Db _ host' => 'localhost, 192.168.1.2 ', // Two database servers
  7. 'Db _ port' => '123 ',
  8. 'Db _ name' => 'thinkphptest', // You do not need to define multiple database names if they are the same. if they are different, they correspond to the server in sequence.
  9. 'Db _ user' => 'root ',
  10. 'Db _ pwd' => 'password ',
  11. // Table prefix
  12. 'Db _ prefix' => 'think _',
  13. // Configure the master-slave database
  14. 'Db _ DEPLOY_TYPE '=> 1, // enable distributed database
  15. 'Db _ RW_SEPARATE '=> ture, // read/write splitting. the first server is the write server by default, and the others are read-only but not written.
  16. );
  17. ?>

Read database file parameters in an action:

  1. $ Hh = C ('Db _ host'); // C can read values in the configuration file
  2. $ Pp = C ('Db _ prefix ');
  3. $ This-> assain ('H', $ hh );
  4. $ This-> assain ('P', $ pp );
  5. $ This-> display ();

Tpl under this action:

Database server address: {$ h}

Database table prefix: {$ p}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.