Home > Article > Backend Development > Connect to mysql database through pdo method in thinkPHP
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