Detailed explanation of the steps to connect the ThinkPHP framework PDO to the database

php中世界最好的语言
Release: 2023-03-26 11:42:01
Original
2141 people have browsed it

This time I will bring you a detailed explanation of the steps of ThinkPHP framework PDOConnecting to the database, and what are the precautions for connecting the ThinkPHP framework PDO to the database. The following is a practical case, let's take a look.

The examples in this article describe the ThinkPHP framework’s connection to database operations based on the PDO method. Share it with everyone for your reference, the details are as follows:

One code

1. Modify the config.php file

<?php
return array(
  &#39;DB_TYPE&#39;=> &#39;pdo&#39;,
  // 注意DSN的配置针对不同的数据库有所区别
  &#39;DB_DSN&#39;=> &#39;mysql:host=localhost;dbname=db_database30&#39;,
  &#39;DB_USER&#39;=>&#39;root&#39;,
  &#39;DB_PWD&#39;=>&#39;root&#39;,
  &#39;DB_PREFIX&#39;=>&#39;think_&#39;,
  // 其他项目配置参数………
  &#39;APP_DEBUG&#39; => true,     // 关闭调试模式
  &#39;SHOW_PAGE_TRACE&#39;=>true,
);
?>
Copy after login

2. Create the controller

<?php
header("Content-Type:text/html; charset=utf-8");  //设置页面编码格式
class IndexAction extends Action{
  public function index(){
    $db = M(&#39;User&#39;);              // 实例化模型类,参数数据表名称,不包含前缀
    $select = $db->select();           // 查询数据
    $this->assign(&#39;select&#39;,$select);       // 模板变量赋值
    $this->display();              // 指定模板页
  }
  public function type(){
    $dba = M(&#39;Type&#39;);              // 实例化模型类,参数数据表名称,不包含前缀
    $select = $dba->select();          // 查询数据
    $this->assign(&#39;select&#39;,$select);       // 模板变量赋值
    $this->display(&#39;type&#39;);         // 指定模板页
  }
}
?>
Copy after login

3. Create the entry file

<?php
define(&#39;THINK_PATH&#39;, &#39;../ThinkPHP&#39;);    //定义ThinkPHP框架路径(相对于入口文件)
define(&#39;APP_NAME&#39;, &#39;App&#39;);       //定义项目名称
define(&#39;APP_PATH&#39;, &#39;./App&#39;);        //定义项目路径
require(THINK_PATH."/ThinkPHP.php");  //加载框架入口文件
App::run();               //实例化一个网站应用实例
?>
Copy after login

4. Create the template file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户信息输出</title>
<link href="ROOT/Public/Css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">
 <tr>
  <td colspan="3" bgcolor="#FFFFFF" class="title" align="center">用户信息</td>
 </tr>
 <tr class="title">
  <td bgcolor="#FFFFFF" width="44">ID</td>
  <td bgcolor="#FFFFFF" width="120">名称</td>
  <td bgcolor="#FFFFFF" width="223">地址</td>
 </tr>
 <volist name=&#39;select&#39; id=&#39;user&#39; >
 <tr class="content">
  <td bgcolor="#FFFFFF"> {$user.id}</td>
  <td bgcolor="#FFFFFF"> {$user.user}</td>
  <td bgcolor="#FFFFFF"> {$user.address}</td>
 </tr>
 </volist>
</table>
</body>
</html>
Copy after login
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>类别输出</title>
<link href="ROOT/Public/Css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">
 <tr>
  <td colspan="3" bgcolor="#FFFFFF" class="title" align="center">类别输出</td>
 </tr>
 <tr class="title">
  <td bgcolor="#FFFFFF" width="44">ID</td>
  <td bgcolor="#FFFFFF" width="120">类别名称</td>
  <td bgcolor="#FFFFFF" width="223">添加时间</td>
 </tr>
 <volist name=&#39;select&#39; id=&#39;type&#39; >
 <tr class="content">
  <td bgcolor="#FFFFFF"> {$type.id}</td>
  <td bgcolor="#FFFFFF"> {$type.typename}</td>
  <td bgcolor="#FFFFFF"> {$type.dates}</td>
 </tr>
 </volist>
</table>
</body>
</html>
Copy after login

Second run Result

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to php Chinese website Other related articles!

Recommended reading:

Detailed explanation of the steps to calculate personal income tax in PHP (with code)

thinkPHP controller variables are in the template Show step details

The above is the detailed content of Detailed explanation of the steps to connect the ThinkPHP framework PDO to the database. 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 admin@php.cn
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!