Php simple method of using PDO in database abstraction layer _php tips

Source: Internet
Author: User

This article illustrates how PHP simply uses PDO in the database abstraction layer. Share to everyone for your reference, specific as follows:

The test code is as follows:

<?php/************************** @Filename: pdotest.php @Content: PDO Operation mysql,access (test) ************************
  **/if ($_get[' db '] = = ' MySQL ') {$dns = ' mysql:host=localhost;dbname=test ';
  $dbuser = ' root ';
  $dbpass = ' root ';
$db = new PDO ($dns, $dbuser, $dbpass); else {$db = new PDO ("Odbc:driver={microsoft Access Driver (*.mdb)};D bq=". GETCWD ().
Test.mdb ");  } if ($_post[' reg ') {$db->exec (INSERT into T_user (name,email) VALUES ('. $_post[' name '). "', '. $_post[' email ']."
  ;");
  Header (' Location: '. $_server[' php_self '));
?> <a href= "pdotest.php" > Return </a> else {$html = ' <div id= ' new ' > <form action= '. $_server[' Request_uri ']. ' "method=" post "> Name: <i Nput type= "text" name= "name" size= "ten"/> Email: <input type= "text" name= "email" size= ""/> <input-type=
  "Submit" Name= "Reg" value= "Register"/> </form> </div>; $re = $db->query ("Select Uid,name,email from T_user order by email ;"); while ($rs = $re->fetch ()) {$userlisthtml. = ' <tr><td> '. $rs [' uid ']. ' </td><td> '. $rs [' name ']. ' </td><td> '. $rs [' email ']. '
  </td></tr> '; 
    $html. = ' <div id= "list" > <table border= "1" > <caption>user list</caption> <thead> <tr><th>ID</th><th>Name</th><th>Email</th></tr> </thead> &
  Lt;tbody> '. $userlisthtml. '
</tbody> </table> </div> ';

 echo $html;?>

Test environment:

php.ini file: Open extension=php_pdo_odbc.dll Remove semicolon open aceess database driver

MySQL defaults to open

Access path:

MySQL Database

Http://192.168.1.21/lava_guess2009/test/pdo/pdotest.php?db=mysql

Aceess Database

http://192.168.1.21/lava_guess2009/test/pdo/pdotest.php

Features: Use a different database as long as you change the connection driver, the code does not make any changes, that is, the benefits of the abstraction layer.

I hope this article will help you with the PHP program design.

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.