PHP connection Access database errors and solutions _php tips

Source: Internet
Author: User
Tags driver manager odbc ole php database sql error stack trace access database

There are two common ways to connect a php+access database.

Recommended code

You need to be aware that the PHP fetch path is Realpath

Copy Code code as follows:

<?php
$connstr = "Driver={microsoft Access DRIVER (*.mdb)}; Dbq= ". Realpath ("Data.mdb");
$connid =odbc_connect ($connstr, "", "", SQL_CUR_USE_ODBC);
$issuetime =date ("y-m-d h:i:s");
$sql = "INSERT INTO test values (" "," ",...)";
$result =odbc_exec ($connid, $sql);
if ($result) echo "successful";
else echo "failed";
?>


The second:

Copy Code code as follows:

? Php
Create an ADO connection
$conn = @new COM ("ADODB. Connection ") or Die (" ADO Connection Failed! ");
$connstr = "Driver={microsoft Access DRIVER (*.mdb)}; Dbq= ". Realpath ("Temp/tempdata.mdb");
$conn->open ($CONNSTR);

To create a recordset query
$rs = @new COM ("ADODB. RecordSet ");
$rs->open ("SELECT * from Blog_content", $conn, 1, 3);
echo $rs->fields["Log_title"]->value; Output Log_title Field
echo "<br/>";
$rs->movenext (); Move the recordset pointer down
echo $rs->fields["Log_title"]->value;
$rs->close ();
?>


Here is the supplementary

1. Through the establishment of ODBC-driven, and then using PHP odbc_connect () function to connect.

Such as:

Copy Code code as follows:

$connstr =driver={microsoft Access DRIVER (*.mdb)}; Dbq= ". $db;
$connid =odbc_connect ($CONNSTR, "username", "password", SQL_CUR_USE_ODBC);

2. Connect with OLE DB, and then invoke the Open method
Such as:

Copy Code code as follows:

$conn =new com ("adodb.connection");
$connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=". $db;
$conn->open ($CONNSTR);

But these two methods how to connect all wrong, go to the Internet to find data, some say is not give everyone permission, some say is access97 and access2000 driver is not the same (is the database is 2000 built, read the driver is 97.) Caused by.

After repeated testing of the original database is caused by the road. Previously in the development of ASP, the database is used to write the address of the relative path, and then use the Server.MapPath () function to obtain his absolute path.

In the development of PHP also extended this habit, with Realpath and database relative path to get the address of the database. For example: $db =realpath (". /db.mdb ");

However, ASP's include functions and PHP include functions in the process of handling the inclusion of files seems different, resulting in PHP in different directories containing the conn.php file connection database appears "common errors can not open the registry key word",

or uncaught exception ' com_exception ' with message ' Source:providerdescription: Validation failed. ' The mistake.

Now put the error message and solution to the people, hope that other friends encounter this situation do not like me so depressed

Error 1

PHP Connection Access Database FAQ

Copy Code code as follows:

Warning:odbc_connect () [
Function.odbc-connect
]: SQL error: [Microsoft][odbc Microsoft Access Driver] Common error cannot open Registry keyword ' temporary (volatile) Jet DSN for process 0xdd0 Thr EAD 0xcb8 DBC 0x14bd024 Jet '. , SQL state S1000 in SQLConnect into E:\wwwroot\phperz.com\phpweb\conn.php on line 8

This error proves that you are using an ODBC access driver, which is the first way to connect to a database $connstr=driver={microsoft Access DRIVER (*.mdb)}; Dbq= ". $db;

And the file you are currently accessing is not in the same directory as your conn.php database connection file, including the relative path for the conn.php, such as include (". /conn.php "), because PHP handles the different files in the Include function and ASP,

caused the error on the database path,

Workaround:

1, check your database path, is not using the Realpath () function plus the relative path to get,

For example: $db =realpath (". /db.mdb ");
If yes, please change the way to get the database address, such as: $_server[' document_root ' to get the root of your site, plus the address of the database

Example: $db =$_server[' Document_root ']. " \db.mdb ";

2, check the permissions, give him everyone permission

3, the new connection mode, Microsoft's ODBC driver there are some unstable bugs, can lead to such errors, it is said that Microsoft itself has given up on the ODBC data source connection mode of support, and recommended that users use the OLE DB method, the connection string should be changed to the following way:

"Provider=Microsoft.Jet.OLEDB.4.0;Data source=". $db;

Error 2:

Fatal error:uncaught exception ' com_exception ' with message ' Source:providerdescription: Validation failed. ' In E:\wwwroot\phperz.com\phpweb\conn.php:7 Stack trace: #0 E:\wwwroot\phperz.com\phpweb\conn.php (7): Com->open (' Provider=micros ... ') #1 ..........


This error indicates that you are using OLE DB to connect to the database, and that the path to the database is also caused.

The solution ibid, with $_server[' Document_root ']. " \db.mdb "; Method get Database Address

Error 3:

Copy Code code as follows:

Warning:odbc_connect () [
Function.odbc-connect
]: SQL error: [MICROSOFT][ODBC Driver Manager] did not find the data source name and did not specify a default driver, SQL State IM002 in SQLConnect in E:\wwwroot\phperz.com\phpweb \conn.php on line 8


ODBC-driven connection to the database, the error is mostly due to your connection string is wrong, the complete connection string should be:

Copy Code code as follows:

$db =$_server[' Document_root ']. " \db.mdb ";
$connstr = "Driver={microsoft Access Driver (*.mdb)};D bq= $db; Uid=admin; Pwd=pass ";
$connid =odbc_connect ($connstr, "admin", "pass", Sql_cur_use_odbc) or Die ("Database open failed! Please contact admin");

If your database does not have a password above UID and PWD can be omitted

Copy Code code as follows:

$connstr = "Driver={microsoft Access Driver (*.mdb)};D bq= $db";
$connid =odbc_connect ($connstr, "", "", Sql_cur_use_odbc) or Die ("Database open failed! Please contact admin");

Error 4:
Fatal error:uncaught exception ' com_exception ' with message ' Source:adodb. Connectiondescription: Provider not found. The program may not be installed correctly. ' in ..............

OLE DB connection method, error reason, and the complete connection string should be:

Copy Code code as follows:

$db =$_server[' Document_root ']. " \db.mdb ";
$conn =new com ("adodb.connection");
$connstr = "Provider=microsoaft.jet.oledb.4.0;data source=". $db;
$conn->open ($CONNSTR);

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.