PHP and MySQL is the best combination of PHP web development, but when you want to migrate other platforms to the PHP platform, you will inevitably encounter porting problems, such as how asp+access platform porting? The first is the PHP connection to the Access database problem, how does PHP connect to an Access database without changing the database?
PHP provides a variety of connectivity database solutions, which explain how to use PHP ADOdb, PDO, ODBC and Access database to establish a connection to the code instance.
Preparatory work
To use the Office tool to create an Access database file
First, use PHP ADODB to connect to an Access database
1, first you need to install PHP ADODB class library.
2. Use PHP ADODB to connect Access database code as follows
1 2 3 4 5 6 7 8 9 10 11 12 13
|
? Include (' adodb5/adodb.inc.php ');
$db =& adonewconnection (' access '); $DSN = "Driver={microsoft Access Driver (*.mdb)};D bq=". Realpath ("Access.mdb"). "; uid=; pwd=; "; $db->connect ($DSN);
$rs = $db->execute (' select * from Web ');
print "<pre>"; Print_r ($rs->getrows ()); print "</pre>"; ?> |
description : With the use of PHP ADODB and MySQL database to establish a connection similar to the first ADODB class library included, and then call Adonewconnection, Connect, Execute creates a connection to the Access database and performs a query operation.
Two, use PHP PDO to connect an Access database
PDO function needs PHP5 above support, you must ensure that the PDO function is installed before using PDO, how to configure installation PDO?
Just locate the Extension_dir in the php.ini configuration file to point to the Extension Library directory address and remove the semicolon before the PDO driver dll you want to use (;), reboot apache,pdo even if installed. This is because we use PDO to connect to the Access database, so at least make sure php_pdo.dll,php_pdo_odbc.dll can support it.
To connect an Access database code instance using PDO
1 2 3 4 5 6 7 8 9
|
? $db = new PDO ("Odbc:driver={microsoft Access Driver (*.mdb)};d bq=". Realpath ("Access.mdb")) or Die ("Connect Error");
$rs = $db->query (' select * from Web ');
print "<pre>"; Print_r ($rs->fetchall ()); print "</pre>"; ?> |
Description : First initialize the PDO object, establish a connection between PHP and the Access database, and then perform the query operation through the PDO query function.
Iii. using ODBC to connect to an Access database
Using ODBC to connect to an Access database code instance
1 2 3 4 5 6 7 8 9 Ten One |
<? $DSN = "Driver=microsoft Access Driver (*.mdb);d bq= ". Realpath (" Access.mdb "); $conn = @odbc_connect ($dsn, "", "", Sql_cur_use_odbc) or Die ("Connect error!"); $sql = "SELECT * from Web"; $rs = @odbc_do ($conn, $sql); while (Odbc_fetch_row ($rs)) { echo "website name:". Odbc_result ($rs, "webname"); echo "<br/> website address:". Odbc_result ($rs, "website"); } Odbc_close ($conn); ? |
Description : First use Odbc_connect to connect to the Access database, the first three parameters are: $DSN, database user name, password, fourth parameter set to Sql_cur_use_ The main purpose of ODBC is to avoid an unexpected error connecting to an Access database, then use ODBC_DO to perform a query operation and invoke Odbc_fetch_row, Odbc_result output query content, and finally use Odbc_close to close the Access database connection.
So far, using PHP ADOdb, PDO, ODBC to connect to the Access database and operation of the code instance is done, through the above example, we can see that PHP is connected to the Access database the same way, using which method depends on the configuration of the PHP environment.
Note : PHP Web Development Tutorials-leapsoul.cn Copyright, reproduced in the form of links to indicate the original source and this statement, thank you.