Detailed description of php access Database Operations and detailed description of access

Source: Internet
Author: User
Tags php and mysql php website

Detailed description of php access Database Operations and detailed description of access

This example describes how to operate the access database in php. We will share this with you for your reference. The details are as follows:

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:

<?php  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 "<pre>";  print_r($rs->GetRows());  print "</pre>";?>

Note: 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

<?php  $db = new PDO("odbc:driver={microsoft access driver (*.mdb)};dbq=".realpath("access.mdb")) or die("Connect Error");  $rs = $db->query('select * from web');  print "<pre>";  print_r($rs->fetchAll());  print "</pre>";?>

Note: First initialize the PDO object, establish a connection between PHP and the Access database, and then use the PDO query function to perform the query operation.

3. Use ODBC to connect to the Access Database

Connect to the Access database code instance using ODBC

<? Php $ 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 "<br/> website address :". odbc_result ($ rs, "website");} odbc_close ($ conn);?>

Note: first, use odbc_connect to connect to the access database. The first three parameters are $ DSN, database username, and password, the fourth parameter is SQL _CUR_USE_ODBC, which is used to avoid unexpected errors when connecting to the Access database. Then, use odbc_do to perform the query operation and call odbc_fetch_row and odbc_result to output the query content, finally, 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.