Home  >  Article  >  Backend Development  >  ThinkPHP framework connects to database operation example based on PDO method

ThinkPHP framework connects to database operation example based on PDO method

不言
不言Original
2018-05-03 16:34:423732browse

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





用户信息输出


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





类别输出


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

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!

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