Home > Article > Backend Development > PHP connection to Access database errors and solutions_PHP tutorial
There are generally two common ways for php+access to connect to the database.
Recommended code
It should be noted that php uses realpath to obtain the path
1. Create an odbc driver and then use PHP’s odbc_connect() function to connect.
For example:
2. Use oledb to connect, and then call the open method to open
, such as:
However, no matter how I 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 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.
This habit was also continued when developing PHP, 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: ProviderDescription: Authentication failed. ' error.
Now I will sort out the error messages and solutions and post them to everyone. I hope other friends who encounter this situation will not be as depressed as me
Error 1
php connection access database FAQ
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"), because PHP handles include The files in the 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: $_SERVER['DOCUMENT_ROOT'] to obtain the root of your website Project, add the address of the database
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:
Fatal error: Uncaught exception 'com_exception' with message 'Source: ProviderDescription: Authentication failed. ' in E:wwwrootphperz.comphpwebconn.php:7 Stack trace: #0 E:wwwrootphperz.comphpwebconn.php(7): com->Open('Provider=Micros...') #1 ...... .........
This error means 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 $_SERVER['DOCUMENT_ROOT']."db.mdb"; method to obtain the database address
Error 3:
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:
If your database does not have a password, the above Uid and Pwd can be omitted
Error 4:
Fatal error: Uncaught exception 'com_exception' with message 'Source: ADODB.ConnectionDescription: 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: