Home >Backend Development >PHP Tutorial >Common errors and solutions when connecting to Access database with PHP_PHP tutorial
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 ClipboardQuoted content: [www.bkjia.com] For example: $connstr=DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".$db;2. Use oledb to connect, and then call the open method to open
For example:
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:
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 ClipboardQuoted content: [www.bkjia.com] Fatal error: Uncaught exception 'com_exception' with message 'Source: ProviderThis 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 ClipboardQuoted 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 8For 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 ClipboardQuoted content: [www.bkjia.com] $db=$_SERVER['DOCUMENT_ROOT']."db.mdb";If your database does not have a password, the above Uid and Pwd can be omitted
Copy to ClipboardQuoted content: [www.bkjia.com] $connstr="Driver={Microsoft Access Driver (*.mdb)};Dbq=$db";Error 4:
Copy to ClipboardQuoted content: [www.bkjia.com] Fatal error: Uncaught exception 'com_exception' with message 'Source: ADODB.Connectionoledb connection method, the error reason is the same as above, the complete connection string should be:
Copy to ClipboardQuoted content: [www.bkjia.com] $db=$_SERVER['DOCUMENT_ROOT']."db.mdb";