Home  >  Article  >  PHP Framework  >  How to connect thinkphp to the database

How to connect thinkphp to the database

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-26 14:20:454197browse

How to connect thinkphp to the database

The examples in this article summarize several common ways for ThinkPHP to connect to the database. Share it with everyone for your reference. The details are as follows:

ThinkPHP has a built-in abstract database access layer, which encapsulates different database operations. We only need to use the public Db class to operate without writing different codes and underlying implementations for different databases. The Db class will automatically call the corresponding database adapter for processing. Current databases include Mysql, MsSQL, PgSQL, Sqlite, Oracle, Ibase and PDO support. If the application needs to use a database, the database connection information must be configured. How many database configuration files are there? Two definition methods:

The first one is to define

in the project configuration file. The code is as follows:

return array(  
 'DB_TYPE'=> 'mysql',   
 'DB_HOST'=> 'localhost',  
 'DB_NAME'=>'thinkphp',   
 'DB_USER'=>'root',   
 'DB_PWD'=>'',   
 'DB_PORT'=>'3306',  
 'DB_PREFIX'=>'think_',  
 // 其他项目配置参数………  
 );

The system recommends this method because Generally, the database access configuration of a project is the same. In this method, the system will automatically obtain it when connecting to the database, without manual connection.

You can define different database connection information for each project, and you can also define the debugging database configuration information in the debugging configuration file (Conf/debug.php). If it is in the project configuration file and debugging mode configuration file If the database connection information is also defined, the latter will take effect in debugging mode and the former will take effect in deployment mode.

Related recommendations: "ThinkPHP Tutorial"

Second, use the DSN method to pass parameters when initializing the Db class

The code is as follows:

$db_dsn = "mysql://username:passwd@localhost:3306/DbName";  
 $db = new Db($db_dsn);

This method is mainly used for manually connecting to the database in the controller, or for creating multiple database connections.

The third method is to use an array to pass parameters.

The code is as follows:

$DSN = array(   
 'dbms'     => 'mysql',    
 'username' => 'username',    
 'password' => 'password',    
 'hostname' => 'localhost',    
 'hostport' => '3306',    
 'database' => 'dbname'   
 );  
 $db = new Db($DSN);

This method is also used for manually connecting to the database, or for Create multiple database connections.

The fourth method is to define it in the model class.

The code is as follows:

protected $connection = array(   
 'dbms'     => 'mysql',    
 'username' => 'username',    
 'password' => 'password',    
 'hostname' => 'localhost',    
 'hostport' => '3306',    
 'database' => 'dbname'   
 );  
 // 或者使用下面的定义  
protected $connection = "mysql://username:passwd@localhost:3306/DbName";

If the connection attribute is defined in a model class, then When instantiating a model object, the database connection information will be used to connect to the database, which is usually used for some data tables located in other databases outside the current database connection.

ThinkPHP does not connect to the database at the beginning, but only when there is a data query operation. In addition, when the system operates the model for the first time, the framework will automatically Connect to the database to obtain the data field information of the relevant model class and cache it.

(Field cache directory: Runtime/Data/_fields)

ThinkPHP supports PDO mode. If you want to use PDO mode to connect to the database, you can refer to the following settings.

We take the project configuration file definition as an example to illustrate:

The code is as follows:

return array(  
 'DB_TYPE'=> 'pdo',   
 // 注意DSN的配置针对不同的数据库有所区别 请参考PHP手册PDO类库部分  
 'DB_DSN'=> 'mysql:host=localhost;dbname=think',  
 'DB_USER'=>'root',   
 'DB_PWD'=>'',   
 'DB_PREFIX'=>'think_',  
 // 其他项目配置参数………  
 );

When using the PDO method, pay attention to check whether the relevant PDO module, DB_DSN, is enabled The parameters are only valid for PDO connection.

The above is the detailed content of How to connect thinkphp to the database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn