Home > Backend Development > PHP Tutorial > In-depth understanding of the basic use of PDO in PHP

In-depth understanding of the basic use of PDO in PHP

WBOY
Release: 2023-04-10 20:38:02
forward
5242 people have browsed it

This article brings you the basic knowledge about pdo. PDO is a major new feature added to PHP5. Our database server is MySQL. All database operations of the program code are performed by a mysql or mysqli() function. Operation, I hope it will be helpful to everyone.

In-depth understanding of the basic use of PDO in PHP

1. Basic knowledge

1. What is PDO?

PDO is the uniform interface of PHP Data Object with a unified interface of PHP operations

2. Why use PDO?

PDO is a major new feature added to PHP5. Our database server is MySQL. All database operations of the program code are operated by a mysql() or mysqli() function. When our database needs to be replaced For example, if it is replaced by SQL, SERVER, PostgreSQL, MS, etc., it is impossible for us to modify all the program codes! So we need to use PDO. PDO helps us solve this problem very well. It is very convenient to use PDO. You only need to modify the data source format and load the corresponding driver file to PHP.ini;

3. What are the characteristics of PDO?

1) Coding consistency

Since the various database extensions available for PHP are written by different publishers, although all extensions provide basically the same features, they are not satisfactory Enough coding consistency. PDO eliminates this inconsistency and provides a single interface that can be used for various databases;

2) Flexibility

Because PDO loads the necessary database drivers at runtime, so No need to reconfigure and recompile PHP every time you use a different database. For example, if the database needs to be switched from SQL to MySQL, you only need to load the PDO_MYSQL driver.

3) Object-oriented features

PDO uses the object-oriented features of PHP5 to achieve more powerful and efficient database communication.

4) High performance

PDO is written in C and compiled to PHP. Compared with other solutions written in PHP, although everything else is the same, it provides more High performance.

2. Basic use of PDO

1. PDO configuration

      1) Modify php.ini and add MySQL PDO extension

                                                               PHP configuration file php.ini, find the line php_pdo_mysql.dll and remove the semicolon

      2) There must be a corresponding extension file in the extension directory

                                                                     php.ini, find extension_dir, this is the directory where our extension exists. First remove the semicolon in front, and then modify the extension directory. My extension directory is in "E:/Web/php/ext", so change it to extension_dir=" E:/Web/php/ext".

3) PDO is connected to different data. There must be different database -drive files, that is, the extension of the configuration file we add
4) Re -start the server, IIS/Apache, I am, I am, I The one is apache, so that the configuration takes effect

2. Data source configuration format

1) Data source format

 (1) Connect to the database, database user name, database name Password

Syntax: $dsn = "Database type: dbname=database name; host=database domain name";

$ User = "Username of Database";

## $ Password = "Password of the Database";


# Series Exam: $ dsn = "mysql: dbname = test;host=127.0.0.1"; 3456";


(2) Declare the object 1 ) Add

                                                                                                                                                                                                     

##                          (2) Generate PDO objects


                                                                                                                                                                                                                                                            Operations, such as insert, delete, update, return the number of affected records


        2) Query

         

(1) Query method one:

                                                                                                                                                                                                                                                                                            ​                         (2) Generate PDO objects

                                         (3)执行查询

                                实现代码:  

<?php
header(&#39;content-type:text/html;charset=utf8&#39;);
//连接数据库
$dsn="mysql:dbname=test;host=127.0.0.1";
//数据库的用户名
$user="root";
//数据库的密码
$password="123456";
//生成PDO对象
$object = new PDO($dsn,$user,$password);

$sql="select * from student";
$result = $object->query($sql);
while($arr=$result->fetch()){
    print_r($arr);
}
Copy after login

查询结果为:


$result的打印结果为:


还可以用:

$data=$result->fetchAll();
print_r($student_info);
Copy after login

输出结果为二维数组:


注释:

1、query()执行查询语句,返回结果集对象;

2、用循环利用fetch()方法逐个的取出记录,返回的是关联数组和索引数组两种数组,和mysql_fetch_array()的返回结果一致

3、fetchAll()方法可以一次取出结果集中所有的数组,以二维数组的形式返回,但仍然是关联数组和索引数组两种数组

数字索引和关联索引都有,属于浪费资源,我们只需要关联索引:还可用用一下方式查询:

$object->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);
$result = $object->query($sql);
$result->setFetchMode(PDO::FETCH_ASSOC);
$result_array = $result->fetchAll();
print_r($result_array);
Copy after login

结果为:


注释:

        setAttribute()方法是设置部分属性,主要属性有:PDO::ATTR_CASE、PDO::ATTR_ERRMODE等,我们设置的是PDO::ATTR_CASE(使用关联索引获取数据)

PDO::CASE_UPPER是设置关联索引为大写,

PDO::CASE_LOWER  --  强制列名为小写

PDO::CASE_NATURAL -- 列名按照原始方式

 PDO::CASE_UPPER -- 强制列名为大写

    setFetchMode()方法设置获取结果集的返回值类型,同样类型还有:

PDO::FETCH_ASSOC --关联数组形式

PDO::FETCH_NUM -- 数字索引形式数组

PDO::FETCH_BOTH --两者数组形式都有

PDO::FETCH_OBJ -- 按照对象的形式,类似于以前的mysql_fetch_object()       

大家如果感兴趣的话,可以点击《PHP视频教程》进行更多关于PHP知识的学习。

The above is the detailed content of In-depth understanding of the basic use of PDO in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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