The test code is as follows:
<? PHP
/**************************
@ Filename: pdotest. php
@ Content: pDO for MySQL and 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"> return </a>
<?
}
Else
{
$ Html ='
<Div id = "new">
<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 id = "list">
<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 and remove the semicolon to open the aceess database driver.
Mysql is enabled 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: to use different databases, you only need to change the connection driver. The Code does not need to be changed, that is, the benefits of the abstraction layer.