PHP connects to access and reads data from an Access database

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags access code data function microsoft mysql name php

PHP connects to access with an ODBC-related function. The code for the connection is as follows:

<?php
$connstr = "Driver=microsoft Access DRIVER (*.mdb);D bq=". Realpath ("Mydb.mdb");
$connid =odbc_connect ($connstr, "", "", SQL_CUR_USE_ODBC);
?>
If you set up a connection, Mydb.mdb is the Access database you want to connect to, then execute the SQL query, similar to MySQL and some different.
$rs =odbc_do ($connid, SQL query);
Odbc_fetch_row ($rs), a little different from MySQL, ODBC does not need to return, only after the execution of Odbc_do (), you can use Odbc_result () to obtain results.
$f 1=odbc_result ($rs, N), which is used to get the results of the query, N starts at 1, represents the first few fields, and can be replaced with field names.
Basically, more functions can refer to the ODBC function section of the PHP manual, according to the MySQL analogy.
Another point to note is that in php.ini there is a limit on reading the contents of the ODBC database, the default is 4KB, if the database has a field size beyond the content will appear, set the method in PHP.ini found
ODBC.DEFAULTLRL, increase the value behind it.
Here's an example:
There is a pic table in the Mydb.mdb, two fields in the table, one is the name, the other is picid, now you want to read out the picid that is not empty and write a text document in a format as follows:

<?php
$connstr = "Driver=microsoft Access DRIVER (*.mdb);D bq=". Realpath ("Mydb.mdb");
$connid =odbc_connect ($connstr, "", "", SQL_CUR_USE_ODBC);
$query =odbc_do ($connid, "select Name, Picid from pic where picid<>");
while (Odbc_fetch_row ($query))
{
$name = Odbc_result ($query, 1);
$id =odbc_result ($query, 2);
$str =$ Name. ' @ '. $id. ' ^';
$fp =fopen ("Picid.txt", "a");
Fwrite ($fp, $STR);
Fclose ($FP);
 }
?>
Another: In Access's SQL query, it is not equal to <> = An error occurs. Like many ODBC databases, Odbc_num_rows ($RS) often returns an error, does not execute correctly, and can be implemented as a while ($odbc _fetch_row ($rs) idea.

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.