The example in this article describes how PHP operates an Access database. Share to everyone for your reference, as follows:
PHP and MySQL are the best combinations in PHP web development, but when you want to migrate other platforms to the PHP platform, you will inevitably encounter porting problems, such as how the Asp+access platform is ported. The first is the PHP connection to access database problems, without changing the database, PHP how to establish a connection with the Access database?
PHP provides a variety of connection database solutions, in this detail how to use PHP ADOdb, PDO, ODBC and Access database to establish a connection code example, as a source.
Preparatory work
To create an Access database file by using Office Tools
I. Using PHP ADODB to connect to an Access database
1, first you need to install PHP ADODB class library.
2. Use PHP ADODB to connect to the Access database code as follows
<?php 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: Similar to using PHP ADODB to establish a connection to the MySQL database, first include the ADODB class library and then call adonewconnection, Connect, execute to establish a connection to the Access database and perform the query operation.
Ii. using PHP PDO to connect to an Access database
PDO functionality requires PHP5 above, you must ensure that PDO is installed before using PDO, how do I configure the installation PDO?
Simply locate the Extension_dir in the php.ini configuration file, point it to the Extension Library directory address, and remove the semicolon (;), restart apache,pdo even if it is installed before you want to use the PDO driver DLL. This is because we are using PDO to connect to an Access database, so at least make sure that Php_pdo.dll,php_pdo_odbc.dll is supported.
Connect to an Access database code instance using PDO
<?php $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
To connect an Access database code instance using ODBC
<?php $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 "site 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, the fourth parameter is set to SQL_CUR_USE_ODBC mainly to avoid an unexpected error connecting to the Access database , then use ODBC_DO to perform the query operation, call Odbc_fetch_row, Odbc_result output query content, and finally close the Access database connection using Odbc_close.
At this point in the use of PHP ADOdb, PDO, ODBC connection Access database and operation of the code example is finished, through the above example, we can see in fact, PHP connection Access database method is similar, which method to use depends on the configuration of the PHP environment.