Home>

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)};dbq =". 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">returns</a>
  <?
}
else
{
  $html="
<div>
  <form action="". $_ Server ["request_uri"]. "" Method="post">
   name:<input type="text" name="name" size="10" />
  email:<input type="text" name="email" size="15" />
  <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>
  <table border="1">
  <caption>user list</caption>
  <thead>
    <tr><th>id</th><th>name</th><th>email</th></tr>
  </thead>
  <tbody>". $Userlisthtml."
  </tbody>
  </table>
</div>
";
}
echo $html;
?>

test environment:

php.ini file:open extension=php_pdo_odbc.dll remove the semicolon and open the aceess database driver

mysql is turned on by default

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:You only need to change the connection driver when using different databases.The code is not used as any change,That is the benefit of the abstraction layer.

php
  • Previous Essential jQuery Practical Skills (Part 2)
  • Next thinkphp how to get client IP