ThinkPHP framework connects to database operation example based on PDO method

不言
Release: 2023-03-25 10:30:02
Original
3706 people have browsed it

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,
);
?>
Copy after login

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');         // 指定模板页
  }
}
?>
Copy after login

3. Create an entry file

Copy after login

4. Create template file





用户信息输出


用户信息
ID 名称 地址
{$user.id} {$user.user} {$user.address}
Copy after login





类别输出


类别输出
ID 类别名称 添加时间
{$type.id} {$type.typename} {$type.dates}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!