PHP connection Access database errors and Workarounds _php tutorial

Source: Internet
Author: User
Tags driver manager ole php database sql error stack trace
There are two common ways to connect a php+access database.

The recommended code

Be aware that PHP gets the path with the Realpath
Copy CodeThe code is as follows:
$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 CodeThe code is as follows:
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);

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 "
";
$rs->movenext (); Move the recordset pointer down
echo $rs->fields["Log_title"]->value;
$rs->close ();
?>


Here are the supplemental

1. Connect by creating an ODBC driver and then using PHP's Odbc_connect () function.

Such as:
Copy the Code code as follows:
$connstr =driver={microsoft Access DRIVER (*.mdb)}; Dbq= ". $db;
$connid =odbc_connect ($CONNSTR, "username", "password", SQL_CUR_USE_ODBC);

2. Connect using OLE DB mode, and then call open method.
Such as:

Copy the 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 error, go online to find information, some say is not give everyone permission, some say is access97 and access2000 driver not the same (that is, the database is 2000 built, read with the driver is 97.) Caused by.

After repeated testing is the result of the database. Used to develop ASP when the address of the database as a relative path, and then use the Server.MapPath () function to obtain his absolute path.

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

However, the ASP's include function and the PHP include function seem different in the way of handling the include file, resulting in PHP containing the conn.php file connection database in different directories "common error cannot open registry key",

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

Now put the error message and the solution to the post to everyone, I hope other friends encounter this situation do not like me depressed

Error 1

PHP Connect Access Database FAQ

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

This error proves that you are using the ODBC Access driver, which is the first method of connecting the 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 used when conn.php, such as include (". /conn.php "), because PHP handles the file and ASP in the Include function,

caused an error on the database path,

Workaround:

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

For example: $db =realpath (". /db.mdb ");
If so, get the database address in a different way, for example, by using: $_server[' document_root ') to get the root of your site, plus the database address

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

2, check permissions, give him everyone permission

3, different connection mode, Microsoft's ODBC driver there are some unstable bugs, it is possible to cause such errors, it is said that Microsoft itself has abandoned the ODBC data source connection mode of support, and recommended that users use 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 an OLE DB-connected database and that it is the path to the database.

The solution is as above, with $_server[' Document_root ']. " \db.mdb "; method gets the database address

Error 3:

Copy the Code code as follows:
Warning:odbc_connect () [
Function.odbc-connect
]: SQL error: [MICROSOFT][ODBC Driver Manager] did not discover 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 because your connection string is incorrectly written, the full connection string should be:

Copy the 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 the Administrator");

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

Copy the 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 the Administrator");

Error 4:
Fatal error:uncaught exception ' com_exception ' with message ' Source:adodb. Connectiondescription: The provider was not found. The program may not be installed correctly. "In ..............

OLE DB connection, the wrong reason, the complete connection string should be:

Copy the 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);

http://www.bkjia.com/PHPjc/327736.html www.bkjia.com true http://www.bkjia.com/PHPjc/327736.html techarticle There are two common ways to connect a php+access database. The recommended code needs to be aware that the PHP fetch path uses the Realpath copy code code as follows: PHP $connstr = "Driver={microsoft Ac ...

  • 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.