This article mainly introduces the operation of the ThinkPHP framework to connect to the database based on the PDO method. It analyzes the related configuration, controller and template calling related operation skills of thinkPHP using the PDO method to connect to the database in the form of a complete example. Friends in need can refer to the following
The example in this article describes the ThinkPHP framework's database connection operation based on PDO method. Share it with everyone for your reference, the details are as follows:
One code
1. Modify the config.php file
'pdo', // 注意DSN的配置针对不同的数据库有所区别 'DB_DSN'=> 'mysql:host=localhost;dbname=db_database30', 'DB_USER'=>'root', 'DB_PWD'=>'root', 'DB_PREFIX'=>'think_', // 其他项目配置参数……… 'APP_DEBUG' => true, // 关闭调试模式 'SHOW_PAGE_TRACE'=>true, ); ?>
2. Create a controller
select(); // 查询数据 $this->assign('select',$select); // 模板变量赋值 $this->display(); // 指定模板页 } public function type(){ $dba = M('Type'); // 实例化模型类,参数数据表名称,不包含前缀 $select = $dba->select(); // 查询数据 $this->assign('select',$select); // 模板变量赋值 $this->display('type'); // 指定模板页 } } ?>
3. Create an entry file
4. Create template file
Second running results
##Related recommendations:ThinkPHP framework Detailed explanation of distributed database connection method
thinkPHP5.0 framework namespace detailed explanation
thinkphp5 method of loading static resource paths and constants
The above is the detailed content of ThinkPHP framework connects to database operation example based on PDO method. For more information, please follow other related articles on the PHP Chinese website!