In PHP website development, PHP and Mysql are the best combination, but when you want to port websites on other platforms to the PHP platform, you will inevitably encounter portability problems, for example, how can I port an ASP + ACCESS platform? The first challenge is how PHP connects to the Access database. without changing the database, how does PHP establish a connection with the Access database? In PHP website development, PHP and Mysql are the best combination, but when you want to port websites on other platforms to the PHP platform, you will inevitably encounter portability problems, for example, how can I port an ASP + ACCESS platform? The first challenge is how PHP connects to the Access database. without changing the database, how does PHP establish a connection with the Access database?
PHP provides a variety of database connection solutions. here, we will explain how to use PHP ADOdb, PDO, ODBC and Access database to establish a connection code instance.
Preparations
Use OFFICE tools to create Access database files
1. use PHP ADOdb to connect to the Access Database
1. First, you need to install the PHP ADOdb class library.
2. use PHP ADOdb to connect to the Access database:
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)}; Dbq =". realpath ("access. mdb"). "; Uid =; Pwd = ;"; $ Db-> Connect ($ dsn );
$ Rs = $ db-> Execute ('select * from web ');
Print""; print_r($rs->GetRows()); print " "; ?> |
Description: Similar to using PHP ADOdb to establish a connection with a Mysql database, first include The ADOdb class library, and then call ADONewConnection, Connect, and Execute to establish a connection with the Access database and perform query operations.
2. use php pdo to connect to the Access Database
The PDO function must be supported by PHP5 or above. before using PDO, you must ensure that the PDO function is installed. how can you configure and install PDO?
In PHP. find extension_dir in The INI configuration file, point it to the extension Library Directory address, remove the semicolon (;) before the PDO driver DLL you want to use, restart Apache, and install PDO. Because we use PDO to connect to the Access database, we must at least ensure that php_pdo.dll and php_pdo_odbc.dll are supported.
Use PDO to connect to the Access database code instance
1 2 3 4 5 6 7 8 9
|
$ Db = new PDO ("odbc: driver = {microsoft access driver (*. mdb)}; dbq = ". realpath ("access. mdb ") or die (" Connect Error ");
$ Rs = $ db-> query ('select * from web ');
Print""; print_r($rs->fetchAll()); print " "; ?> |
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.
3. use ODBC to connect to the Access Database
Connect to the Access database code instance using ODBC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
$ Dsn = "DRIVER = Microsoft Access Driver (*. mdb); dbq =". 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" 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 username and password, and the fourth parameter is set to SQL _CUR_USE_ODBC to avoid unexpected errors when connecting to the Access database; then, use odbc_do to perform the query operation, call odbc_fetch_row and odbc_result to output the query content, and use odbc_close to close the Access database connection.
Now, we have introduced the code instances for connecting to and operating the Access database using PHP ADOdb, PDO, and ODBC. through the above examples, we can see that the methods for connecting to the Access database using PHP are similar, which method is used depends on the configuration of the PHP environment.