Home Backend Development PHP Tutorial Detailed explanation of the steps to connect the ThinkPHP framework PDO to the database

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

May 17, 2018 pm 02:48 PM
php thinkphp database

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

1

2

3

4

5

6

7

8

9

10

11

12

13

<?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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?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

1

2

3

4

5

6

7

<?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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<!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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<!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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1229
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

See all articles