Three ways to connect the PDO database

Source: Internet
Author: User
Tags dsn getmessage vars

Before using PDO to connect to a database, install and configure the PDO

Base. PHP is as follows:

[PHP]View PlainCopy
    1. <strong><span style="FONT-SIZE:18PX;" ><?php
    2. Header (' Content-type:text/html;charset=utf-8 ');
    3. Array Debug functions
    4. function Show_bug ($msg) {
    5. echo ' <pre> ';
    6. Print_r ($msg);
    7. echo ' </pre> ';
    8. }
    9. ?></span></strong>

1.pdo linking the database via parametric form

[HTML]View PlainCopy
  1. <strong><span style="FONT-SIZE:18PX;"  >include_once "base.php";
  2. try{
  3. $DSN is the data source
  4. $dsn=' MYSQL:HOST=LOCALHOST;DBNAME=IMOOC ';
  5. $username=' root ';
  6. $passwd=';
  7. $pdo=New PDO ($DSN, $username, $passwd);
  8. If the connection is successful, you get the PDO object.
  9. Show_bug ($PDO);
  10. }catch (Pdoexception $e) {
  11. Echo $e->getmessage ();
  12. }</span></strong>

2.pdo connecting the database by URI
Create a new Dsn.txt file with the following:

[PHP]View PlainCopy
  1. <strong><span style="FONT-SIZE:18PX;" ><?php
  2. Include_once "base.php";
  3. PDO connects to database by URI
  4. try{
  5. $dsn =' uri:file://d:\wamp\www\muke\pdo\dsn.txt ';
  6. $username =' root ';
  7. $passwd =";
  8. $pdo =new PDO ($dsn,$username,$passwd);
  9. Show_bug ($pdo);
  10. }catch (pdoexception $e) {
  11. echo $e->getmessage ();
  12. }
  13. ?></span></strong>

3.pdo connect to the database as a configuration file

[PHP]View PlainCopy
  1. <strong><span style="FONT-SIZE:18PX;" ><?php
  2. Include_once "base.php";
  3. PDO connects to a database by configuration file form
  4. Configuring in PHP.ini
  5. try{
  6. $dsn =' Imooc ';
  7. $username =' root ';
  8. $passwd =";
  9. $pdo =new PDO ($dsn,$username,$passwd);
  10. Show_bug ($pdo);
  11. }catch (pdoexception $e) {
  12. echo $e->getmessage ();
  13. }
  14. ?></span></strong>


Of the above three kinds, we recommend using the first, through the Parameter form link database

Three ways to connect the PDO database

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.