Home>Article>Backend Development> PHP database learning: How to use PDO to connect to the database?
In the previous article, I brought you "How to get the number of rows in the query results in PHP database learning?"", which introduces in detail how to obtain the number of rows of query results in the PHP database. In this article, let's take a look at the relevant knowledge of PDO in PHP. I hope everyone has to help!
In the previous articles on PHP database learning, we introduced some related knowledge about the mysqli class in PHP. Among them, we know that mysqli can only support MySQL database. At this time we It's not possible to connect to other databases. So how can we connect to other databases? By what means? At this time we need to introduce another database-related class in our PHP, which is the PDO class. Let’s take a look at it next.
What is PDO
PDO is the abbreviation of PHP data object. To be precise, PDO is the definition of PHP access database An interface that can perform queries and obtain data through the same function it provides. This interface is lightweight and consistent, and can be accessed and executed through the same function no matter what database you are using. Such an operation greatly simplifies the operation of the database, and we do not need to make changes based on different differences in the database.
In this case, with PDO, you no longer need to use a series of functions ofmysqli_*
. You only need to use the methods in PDO to operate the database.
We can think of PDO as a "database access abstraction layer", which serves to unify the access interfaces of various databases. PDO will unify the common features of various databases through a lightweight, clear, and convenient function to achieve the greatest degree of abstraction and compatibility of PHP scripts.
The PDO extension is modular and can load drivers for user database backends at runtime without having to recompile or reinstall the entire PHP program.
How to turn on PDO
By default, PDO is turned on in PHP, but some drivers for a certain database If you want to start it, you still need to perform the corresponding opening operation.
Let’s take windows as an example and find the relevant configuration information of PDO in thephp.ini
configuration file:
Among them To enable the corresponding configuration, you only need to remove the semicolon;
in front of the configuration item, and then restart the Apache server.
After the configuration is completed, you can check whether it is opened successfully throughphpinfo()
. The example is as follows:
The following results can be found in the output results:
So we have opened PDO through the php.ini configuration file. Now that pdo has been opened, the following is how to connect to the database. Next, let’s take a look at PHP How to use PDO to connect to the database.
PHP uses PDO to connect to the database
#PHP wants to use PDO to connect to the database to interact with different databases. At this time The member methods in the PDO object agree with the access interfaces of various databases to achieve the purpose of interacting with different databases. So before using PDO to interact with the database, we must first create a PDO object, and then connect to the database through the object's constructor. The syntax format of this constructor is as follows:
PDO::__construct(string $dsn[, string $username [, string $password [, array $driver_options]]])
It should be noted that:
$dsn
represents the data source The name, or DSN, contains the information requested to connect to the database. Typically a DSN consists of the name of the PDO driver, followed by a colon, and optionally the driver's database connection information.
$username
represents an optional parameter, used to represent the user name in the DSN string;
$password
represents optional parameters, used to represent the password in the DSN string;
$driver_options
represents optional parameters, a specific driver Key/value array of connection options.
You can call the constructor method in a variety of ways to create a PDO object. Let's take connecting to the MySQL database as an example to introduce the various ways of calling the constructor method.
Examples are as follows:
getMessage(); } ?>
上述示例中,将参数嵌入到构造函数中,在 DSN 字符串中加载 mysql 驱动程序并指定了两个可选参数:第一个是数据库名称,第二个是数据库地址。其他的驱动程序会同样以不同的方式解释它的 DSN,如果无法加载驱动程序,或者发生了连接失败,则会抛出一个 PDOException,以便可以决定如何最好地处理该故障。
其中的参数也可以储存在一个本地或者远程的文件中,然后在构造函数中引用这一文件,示例如下:
首先在本地的文件中创建一个dsn.txt的文件,其中的内容和路径如下:
然后输入示例如下:
getMessage(); } ?>
由此我们便通过吧擦书储存在文件里,然后通过构造函数完成了数据库的调用。
大家如果感兴趣的话,可以点击《PHP视频教程》进行更多关于PHP知识的学习。
The above is the detailed content of PHP database learning: How to use PDO to connect to the database?. For more information, please follow other related articles on the PHP Chinese website!