Connect to multiple databases using PDO

Source: Internet
Author: User
Tags db2 dsn new set stmt

Before PHP 5, you would need to use a series of functions such as MySQL or mysqli to operate the database before you could connect to the MySQL database. For example, we use the MySQL series database functions for query operations, the corresponding sample code is as follows:

<?php
Create a database connection
$link = mysql_connect (' 127.0.0.1 ', ' username ', ' password ') or Die (' Connect database failed! ');
mysql_select_db (' Test ', $link); Select the specified database
$res = mysql_query (' SELECT ID, body from demo ', $link);
while ($row = Mysql_fetch_row ($res)) {
echo "Id= $row [0],body= $row [1]<br/>";
}
Mysql_free_result ($res);
Mysql_close ($link);
?>

Similarly, if we are going to use PHP to connect to Oracle, DB2, or other databases, as shown in the code above, we also need to use them for example Db2_connect, Db2_fetch_row, Db2_close, Oci_connect, Oci_fetch_ Many PHP functions, such as row, Oci_close, and so on, begin with the corresponding database.

If we need to connect multiple databases in our program, we have to write multiple tool classes (or functions) that operate on different databases. Obviously, such a database access API seems unfriendly to PHP developers, adding a lot of annoyance to the development of programs that involve multiple databases.

However, this situation has improved since PHP 5. In PHP 5 and later, PHP provides us with a new set of database access API extensions--php Data Objects (PDO). The PDO extension Library defines a lightweight, consistent interface for the PHP Access database. It is an abstract data access interface, which we can easily use to unify the operation of different databases. For example, we re-rewrite the above example as PDO, with the following code:

<?php

$type = ' MySQL '; Database type
$db _name = ' Test '; Database name
$host = ' 127.0.0.1 ';
$username = ' root ';
$password = ' ready ';

$DSN = "$type: host= $host;d bname= $db _name";
try {
$pdo = new PDO ($DSN, $username, $password);
} catch (Exception $e) {
Die (' Connect database failed! ');
}
$stmt = $pdo->query (' SELECT ID, body from demo ');
while ($row = $stmt->fetch ()) {
echo "Id= $row [0],body= $row [1]<br/>";
}
$pdo = null;
?>

At this point, if we need to migrate the database from MySQL to DB2 or Oracle, the corresponding changes in the PHP program are very simple, there is no need to make more changes elsewhere, we just need to set the variable $type to ODBC or OCI.
Note

So far, the PHP PDO extension is connected to DB2 and needs to be connected in ODBC, so $type needs to be ODBC-set.

PHP PDO Extension Library is just a data access abstraction interface, if you want to connect a different database, you need to add the corresponding database driver to implement the PDO interface. Of course, PHP has already prepared some database-driven extensions (stored in the PHP installation directory/ext directory), such as Php_pdo_mysql.dll (for MySQL), Php_pdo_oci.dll (for Oracle), Php_pdo_ Odbc.dll (for databases accessed in ODBC form). Although different database-driven implementations are not the same, but for our developers, it is the same way to access the database, PDO in the middle has helped us to do the appropriate conversion processing work.

In the php.ini configuration file, the default configuration provided by the official does not enable PDO extension (MySQL, mysqli extension is not started, we need to enable the corresponding PDO extension library in php.ini:

, which database you need to use, remove the comment symbol in front of the line that corresponds to the configuration file ";"
Extension=php_pdo_mysql.dll
; Extension=php_pdo_oci.dll
; Extension=php_pdo_odbc.dll

For more information about PDO usage, please refer to the official PHP documentation.

Connect to multiple databases using PDO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.