PHP Access database instance tutorial

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags access asp client code connect connection database function how to

When the client site is ASP + ACCESS platform how to transplant to the PHP platform? First of all, we want to solve the PHP Access database connection problem, without changing the database, PHP how to establish a connection with the Access database?

PHP provides a variety of connection database solutions, explain in detail how to use PHP ADOdb, PDO, ODBC and Access database connection instance code.

Ready to work


First, use PHP ADOdb to connect to the Access database

1, first of all you need to install PHP ADOdb class library.

2, use PHP ADOdb Access database code is as follows

<?
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>";
?>

Description: Using PHP ADOdb and Mysql database to establish a connection similar to the first ADOdb library included, and then call ADONewConnection, Connect, Execute and Access database to establish a connection and perform the query operation.

Second, use PHP PDO to connect Access database

PDO function need PHP5 above support, before using PDO you must ensure that the PDO function is installed, how to configure the installation of PDO?

Just find the extension_dir in your PHP.INI configuration file, point it to the extension's directory address, and remove the semicolon (;) before the PDO driver DLL you want to use. Restart Apache and PDO is installed. Here we use PDO to connect to the Access database, so at least to ensure php_pdo.dll, php_pdo_odbc.dll can support.

Use PDO to connect to the Access database code instance


<?
$ 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>";
?>

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

Third, use ODBC to connect to the Access database

Use ODBC to connect to the Access database code instance


<?
$ 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 access database access, the first three parameters are: $ DSN, database user name, password, the fourth parameter is set to SQL_CUR_USE_ODBC Mainly in order to avoid accidental errors connecting Access database; Then use odbc_do query operation, And call odbc_fetch_row, odbc_result output query content, and finally close the Access database connection using odbc_close.

At this point the use of PHP ADOdb, PDO, ODBC access database and the operation of the code example is introduced, through the above example, we can see in fact PHP Access database access methods are more or less the same, which method to use depends on the configuration of the PHP environment.

Related Article

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.