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.