Home  >  Article  >  Backend Development  >  Connect to mysql database through pdo method in thinkPHP

Connect to mysql database through pdo method in thinkPHP

巴扎黑
巴扎黑Original
2016-11-08 10:22:131033browse

First find the config.php file in the Conf subdirectory of the Common directory in the project file directory

Add the following code:
'DB_TYPE' => 'pdo',
'DB_USER' => 'root',
'DB_PWD ' => 'Database password',
'DB_PREFIX' => 'think_', Table prefix
'DB_DSN' => 'mysql:host=localhost;dbname=connected database name; charset=UTF8'



Note: charset=UTF8 instead of UTF-8, and an error will be reported when writing UTF-8 web pages.

There is another method: for model instantiation using Model(), you can use the third part of:

Model(['Model name'], ['Data table prefix'], ['Database connection information']); Define the database in each parameter database connection information

For example: $user = new Model('User','think_','mysql://root:123456@localhost/thinkphp');

The format of the database connection information is: database Type://username:password@database address:database port/database name#Character set

This example should be modified according to the actual situation


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