Three ways to connect a database with PDO

Source: Internet
Author: User
Tags dsn getmessage vars

The appearance of PDO is to solve the problem that the connection between PHP and each database has its own function, and its high abstraction makes it extremely convenient to use. Because the most common collocation is php+mysql, so here is the example of connecting Mysql. first, the Parameter form [PHP]View Plaincopyprint?
  1. try{
  2. $dsn = ' Mysql:host=localhost;dbname=myblog ';
  3. $username = ' root ';
  4. $passwd = ' 123456 ';
  5. $pdo = new PDO ($dsn, $username, $passwd);
  6. Var_dump ($pdo);
  7. }catch (pdoexception $e) {
  8. echo $e->getmessage ();
  9. }
try{$dsn      = ' Mysql:host=localhost;dbname=myblog '; $username = ' root '; $passwd   = ' 123456 '; $pdo      = new PDO ($ DSN, $username, $passwd); Var_dump ($PDO);} catch (Pdoexception $e) {echo $e->getmessage ();}
second, the URI form [PHP]View Plaincopyprint?
  1. try{
  2. $dsn = ' uri:file://f:\wamp\www\myblog\dsn.txt ';
  3. $username = ' root ';
  4. $passwd = ' 123456 ';
  5. $pdo = new PDO ($dsn, $username, $passwd);
  6. Var_dump ($pdo);
  7. }catch (pdoexception $e) {
  8. echo $e->getmessage ();
  9. }
  10. Dsn.txt
  11. Mysql:host=localhost;dbname=myblog
try{$dsn      = ' uri:file://f:\wamp\www\myblog\dsn.txt '; $username = ' root '; $passwd   = ' 123456 '; $pdo      
Third, the configuration file mode [PHP]View Plaincopyprint?
  1. try{
  2. $dsn = ' Mypdo ';
  3. $username = ' root ';
  4. $passwd = ' 123456 ';
  5. $pdo = new PDO ($dsn, $username, $passwd);
  6. Var_dump ($pdo);
  7. }catch (pdoexception $e) {
  8. echo $e->getmessage ();
  9. }
  10. Add the following code anywhere in the php.ini file, and save the restart server
  11. pdo.dsn.mypdo="Mysql:host=localhost;dbname=myblog"
try{$dsn      = ' mypdo '; $username = ' root '; $passwd   = ' 123456 '; $pdo      = new PDO ($DSN, $username, $passwd); var_dump ($PDO);} catch (Pdoexception $e) {echo $e->getmessage ();} Where you add the following code anywhere in the php.ini file, and save the restart server pdo.dsn.mypdo= "Mysql:host=localhost;dbname=myblog"

Three ways to connect a database with PDO

Related Article

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.