This article mainly introduces thinkphp's skills for configuring database connection. The example describes how to configure the database and controller to connect to the database in the same directory at the ThinkPHP portal. For more information, see
This article mainly introduces thinkphp's skills for configuring database connection. The example describes how to configure the database and controller to connect to the database in the same directory at the ThinkPHP portal. For more information, see
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.
The 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:
The 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 controller, add the following code:
The 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.
,