Using the PDO constructor to connect to the database and DSN details

Source: Internet
Author: User
Tags try catch
using the PDO constructor to connect to the database and DSN details

PDO is a newly added database abstraction layer in PHP5, in order to solve the problem of accessing the unified interface of different databases. Similar to Pear: operation of:D Class B and ADODB classes, but it is directly encapsulated in the PHP extension and can be freely selected for use.

In the previous article, what is PDO? The overview of PDO and how to install PDO, we describe what PDO is, describes the features of PDO, and how to install it, so this article describes the PDO constructor connection database and DSN details!

PDO Constructor Connection Database

In PDO, the connection to the database needs to instantiate the PDO constructor, and the PDO constructor has the following syntax:

_construct (String $dsn [, String $username [, String $password [, array $driver _options]])

The parameters of the constructor are described as follows:

DSN: Data source name, including host name port number and database name.

Username: The user name of the connection database.

Password: the password to connect to the database.

Driver_options: Additional options for connecting to the database.

Then we use the example below to explain, through the PDO connection MySQL database, the specific code is as follows:

<?phpheader ("content-type:text/html; Charset=utf-8 ");    Set the encoding format of the page $dbms = "MySQL";                                  The type of database $dbname = "PHP_CN";                                The database name used is $user = "root";                                   Database user name used $pwd = "root";                                    The database password used $host = "localhost";                              Host name used $dsn  = "$dbms: host= $host;d bname= $dbName";       try{                                             //catch exception    $pdo = new PDO ($DSN, $user, $pwd);             Instantiate object    echo  "PDO connection Database succeeded";} catch (Exception $e) {    echo $e->getmessage (). " <br> ";}

The results of the page output are as follows:

Note: If you fill in the wrong place above, you will throw an exception through the Try Catch statement!

DSN detailed

What is DSN?

The DSN is the acronym for the Data Source name (the datasource names). DSN provides the information needed to connect to the database. The DSN for PDO includes 3 parts: PDO driver name (e.g. MySQL, SQLite, or pgsql), colon, and driver-specific syntax. Each database has its own specific driver syntax.

When using a different database, it must be clear that the database server is completely independent of PHP and is an entity. However, the actual situation may be that the database server is not on the same computer as the Web server, and the host name in the DSN needs to be modified when the database is connected through PDO.

Because the database server is only listening for connection requests on specific ports. Each database server has a default port number (MySQL is 3306), but the database administrator can modify the port number, so it is possible that PHP cannot find the port number for the database, so you can include the port number in the DSN. Like what:

$DSN = "Mysql:host=127.0.0.1;port=3306;dbname=admin";

Because a database may have multiple databases at the same time, when you connect to a database through a DSN, you usually include the database name, which ensures that the user wants the database, not the other database!

about using the PDO constructor to connect to the database and DSN details We introduce here, you can test it locally, then we will introduce you to the PDO in the implementation of the SQL statement of several methods, please read "PDO in the implementation of SQL statements three ways"!

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.