Home  >  Article  >  Backend Development  >  Common errors and solutions when connecting to Access database with PHP_PHP tutorial

Common errors and solutions when connecting to Access database with PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:53:351073browse

There are generally two common ways for php+access to connect to the database

1. Create an odbc driver and then use PHP’s odbc_connect() function to connect

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] For example: $connstr=DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".$db;
$connid=odbc_connect($connstr,"username","password",SQL_CUR_USE_ODBC);

2. Use oledb to connect, and then call the open method to open
For example:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] $conn=new com("ADODB.connection");
$connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=".$db;
$conn->Open($connstr );

However, no matter how to connect these two methods, there are errors. When I looked for information on the Internet, some said that Everyone permission was not granted, and some said that the drivers of access97 and access2000 were different (that is, the database was built in 2000 and was used for reading. The driver is caused by 97.).

After repeated testing, it turned out to be caused by the path of the database. In the past, when developing asp, I used to write the address of the database as a relative path, and then use the server.mappath() function to obtain its absolute path.
When developing php This habit is also continued, using realpath plus the relative path of the database to obtain the address of the database. For example: $db=realpath("../db.mdb");

However, the include function of asp and the include function of php seem to process included files in different ways, which causes "common error cannot open the registry keyword" when php includes the conn.php file in different directories to connect to the database.

Or Uncaught exception 'com_exception' with message 'Source: Provider
Description: Verification failed. ' error.
Now I will sort out the error information and solutions and post them to everyone. I hope other friends will not be as depressed as me when they encounter this situation.

php connection access database FAQ
Error 1:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Microsoft Access Driver]Common errors Cannot open the registry keyword 'Temporary (volatile) Jet DSN for process 0xdd0 Thread 0xcb8 DBC 0x14bd024 Jet'. , SQL state S1000 in SQLConnect in E:wwwroot\phpwebconn.php on line 8

If this error occurs, it proves that you are using the odbc access driver, which is the first method of connecting to the database mentioned above $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. The relative path used when including conn.php, such as include("../conn.php"), is due to PHP processing The files in the include function are different from those in asp,

Caused an error in the database path,

Solution:
1. Check the path of your database to see if it is obtained by using the realpath() function plus a relative path.

For example: $db=realpath("../db.mdb");

If so, please use another method to obtain the database address, such as using: $_SERVER['DOCUMENT_ROOT'] to obtain the root directory of your website, and then add the database address,

Example:$db=$_SERVER['DOCUMENT_ROOT']."db.mdb";

2. Check the permissions and give him Everyone permission

3. Change the connection method. Microsoft's odbc driver has some unstable bugs, which may cause such errors. It is said that Microsoft itself has given up support for the odbc data source connection method and recommends users to use the oledb method. The connection string should be changed to the following:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=".$db;

Error 2:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] Fatal error: Uncaught exception 'com_exception' with message 'Source: Provider
Description: Authentication failed. ' in E:wwwroot\phpwebconn.php:7 Stack trace: #0 E:wwwroot\phpwebconn.php(7): com->Open('Provider=Micros...') #1 ...... .........

This error indicates that you are using oledb to connect to the database, and it is also caused by the path of the database.
The solution is the same as above, use the $_SERVER['DOCUMENT_ROOT']."db.mdb"; method to obtain Database address

Error 3:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in E:wwwroot\phpwebconn. php on line 8

For databases connected using the odbc driver, the error may be caused by your connection string being written incorrectly. The complete connection string should be:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] $db=$_SERVER['DOCUMENT_ROOT']."db.mdb";
$connstr="Driver={Microsoft Access Driver (*.mdb)};Dbq=$db;Uid=Admin;Pwd=pass" ;
$connid=odbc_connect($connstr,"admin","pass",SQL_CUR_USE_ODBC) or die("Database opening failed! Please contact the administrator");

If your database does not have a password, the above Uid and Pwd can be omitted

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] $connstr="Driver={Microsoft Access Driver (*.mdb)};Dbq=$db";
$connid=odbc_connect($connstr,"","",SQL_CUR_USE_ODBC) or die("Database opening failed! Please contact the administrator");

Error 4:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] Fatal error: Uncaught exception 'com_exception' with message 'Source: ADODB.Connection
Description: Provider not found. The program may not be installed correctly. 'in......................

oledb connection method, the error reason is the same as above, the complete connection string should be:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] $db=$_SERVER['DOCUMENT_ROOT']."db.mdb";
$conn=new com("ADODB.connection");
$connstr="Provider=Microsoaft.Jet.OLEDB.4.0; Data Source=".$db;
$conn->Open($connstr);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364756.htmlTechArticleThere are generally two common ways for php+access to connect to the database 1. By establishing an odbc driver, and then using php's odbc_connect () function connection Copy to Clipboard quoted content: [www.veryhuo.com] Such as:...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn