Thinkphp Database Configuration and connection skills

Source: Internet
Author: User

Thinkphp Database Configuration and connection skills

This example describes how to configure thinkphp to connect to a database. The specific method is as follows:

1. Add the database configuration information to config. inc. php in the same directory as the thinkphp entry file.
Copy codeThe Code is as follows: <? Php
Return array (
'Db _ type' => 'mysqli ',
'Db _ host' => 'localhost ',
'Db _ name' => 'Demo ',
'Db _ user' => 'root ',
'Db _ pwd' => 'root ',
'Db _ prefix' => 'think _',
);
?>
Add the following code to the config. php file of the project directory:
Copy codeThe Code is as follows: <? Php
$ Arr1 = array (
// 'Config maps '=> 'configuration value'
'Url _ model' => 2,
'App _ debug' => true,
'Log _ RECORD '=> true,
......
);
$ Arr2 = include 'config. inc. php ';
Return array_merge ($ arr1, $ arr2 );
?>
You can also add the above Code in the background file. After the connection information of the database configuration is changed, you only need to modify config. inc. the information in the PHP file allows you to modify the database connection configuration of the front-end and back-end at the same time.

2. If you want to connect to another database in the ControllerYou can add the following code:
Copy codeThe Code is as follows: $ arrr = array (
'Dbms '=> 'mysqli', // note that the key name in the group in this book cannot be changed.
'Username' => 'Google ',
'Password' => 'hello ',
'Hostname' => '2017. 0.0.1 ',
'Database' => 'mydbname ',
);
$ Db = new Db ($ arrr );

I hope this article will help you with ThinkPHP framework programming.

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.