Home  >  Article  >  Backend Development  >  What is PHP's PDO?

What is PHP's PDO?

Guanhui
GuanhuiOriginal
2020-06-24 11:51:103775browse

What is PHP's PDO?

What is the PDO extension for PHP?

PDO is a data object extension of PHP. This extension defines a lightweight consistent interface for PHP to access databases, and provides a data access abstraction layer that allows users to access databases regardless of which database they use. , you can use the same method to query and obtain data.

Recommended PDO video tutorial: "PDO Database Abstraction Layer"

PDO Installation

You can check whether the PDO extension is installed through PHP's phpinfo() function.

Installing PDO on Unix systems

On Unix or Linux you need to add the following extensions:

extension=pdo.so

Windows users

PDO and all major Drivers are distributed with PHP as shared extensions. To activate them, simply edit the php.ini file and add the following extension:

extension=php_pdo.dll In addition, there are various databases corresponding to the following Extension:

;extension=php_pdo_firebird.dll
;extension=php_pdo_informix.dll
;extension=php_pdo_mssql.dll
;extension=php_pdo_mysql.dll
;extension=php_pdo_oci.dll
;extension=php_pdo_oci8.dll
;extension=php_pdo_odbc.dll
;extension=php_pdo_pgsql.dll
;extension=php_pdo_sqlite.dll

After setting these configurations, we need to restart PHP or the Web server.

Next let’s take a look at specific examples. The following is an example of using PDO to connect to a MySql database:

<?php
$dbms=&#39;mysql&#39;;     //数据库类型
$host=&#39;localhost&#39;; //数据库主机名
$dbName=&#39;test&#39;;    //使用的数据库
$user=&#39;root&#39;;      //数据库连接用户名
$pass=&#39;&#39;;          //对应的密码
$dsn="$dbms:host=$host;dbname=$dbName";
try {
    $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象
    echo "连接成功<br/>";
    /*你还可以进行一次搜索操作
    foreach ($dbh->query(&#39;SELECT * from FOO&#39;) as $row) {
        print_r($row); //你可以用 echo($GLOBAL); 来看到这些值
    }
    */
    $dbh = null;
} catch (PDOException $e) {
    die ("Error!: " . $e->getMessage() . "<br/>");
}
//默认这个不是长连接,如果需要数据库长连接,需要最后加一个参数:array(PDO::ATTR_PERSISTENT => true) 变成这样:
$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
?>

Recommended tutorial: "PHP

The above is the detailed content of What is PHP's PDO?. 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