Home > Article > Backend Development > 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='mysql'; //数据库类型 $host='localhost'; //数据库主机名 $dbName='test'; //使用的数据库 $user='root'; //数据库连接用户名 $pass=''; //对应的密码 $dsn="$dbms:host=$host;dbname=$dbName"; try { $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象 echo "连接成功<br/>"; /*你还可以进行一次搜索操作 foreach ($dbh->query('SELECT * from FOO') 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!