I changed an asp + access website to php + access two days ago, which made me very depressed when I connected the database, through a large number of articles related to Baidu, we have finally solved two common methods for connecting php + access to the database.
Recommended Code
Note that php uses realpath to obtain the path.
The 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 ";
?>
II:
The 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 record set query
$ Rs = @ new COM ("ADODB. RecordSet ");
$ Rs-> Open ("select * from blog_Content", $ conn, 1, 3 );
Echo $ rs-> Fields ["log_Title"]-> Value; // output the log_Title field
Echo"
";
$ Rs-> Movenext (); // move the record set pointer down
Echo $ rs-> Fields ["log_Title"]-> Value;
$ Rs-> close ();
?>
Below is a supplement
1. establish an odbc driver and connect it with the odbc_connect () function of php.
For example:
The code is as follows:
$ 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:
The code is as follows:
$ Conn = new com ("ADODB. connection ");
$ Connstr = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =". $ db;
$ Conn-> Open ($ connstr );
However, an error occurred while connecting the two methods. when I went to the Internet to find information, I said that I did not grant the Everyone permission, and some said that the access97 and access2000 drivers were different (that is, the database was built on 2000, the driver used for reading is 97 .).
The original path of the database was tested repeatedly. I used to write the database address as a relative path when developing asp, and then use the server. mappath () function to obtain its absolute path.
This habit is also continued during php Development. We use realpath and the relative path of the database to obtain the address of the database. for example: $ db = realpath ("../db. mdb ");
However, asp's include function and php's include function seem to be different in the processing of include files, resulting in php's inclusion of conn in different directories. the PHP file is connected to the database and "common errors cannot open the registry keyword ",
Or Uncaught exception 'com _ exception' with message' Source: ProviderDescription: Verification failed. 'Error.
Now I will sort out the error information and solutions and post it to you. I hope other friends will not be as depressed as I do when this happens.
Error 1
Php access database FAQs
The code is as follows:
Warning: odbc_connect ()[
Function. odbc-connect
]: SQL error: [Microsoft] [ODBC Microsoft Access Driver] common errors do not enable the registry keyword 'temporary (volatile) Jet DSN for process 0xdd0 Thread 0xcb8 DBC 0x14bd024 jet '., SQL state S1000 in SQLConnect in E: \ wwwroot \ phperz.com \ phpweb \ conn. php on line 8
This error indicates that you are using the odbc access DRIVER, which is the first method to connect to the database. $ connstr = Driver = {Microsoft Access DRIVER (*. mdb)}; DBQ = ". $ db;
And the file you are currently accessing and your conn. the php database connection file is not in the same directory, including conn. the relative path used in php, such as include (".. /conn. php "), because php processes different files in the include function from asp,
Causes a database path error,
Solution:
1. check whether the path of your database is obtained by adding a relative path to the realpath () function,
For example: $ db = realpath ("../db. mdb ");
If yes, please use another method to obtain the database address. for example, use $ _ SERVER ['document _ root'] to obtain the ROOT object of your website, and add the database address.
Example: $ db = $ _ SERVER ['document _ root']. "\ db. mdb ";
2. check the permission and grant it the Everyone permission.
3. in another connection mode, the Microsoft odbc driver has some unstable bugs, which may lead to such errors. it is said that Microsoft has abandoned support for the odbc data source connection mode, we recommend that you use oledb to change the connection string as follows:
"Provider = Microsoft. Jet. OLEDB.4.0; Data Source =". $ db;
Error 2:
Fatal error: Uncaught exception 'com _ exception' with message' Source: ProviderDescription: Verification 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 the oledb method to connect to the database, and it is also caused by the database path.
The solution is the same as above. use $ _ SERVER ['document _ root']. "\ db. mdb"; to obtain the database address.
Error 3:
The code is as follows:
Warning: odbc_connect ()[
Function. odbc-connect
]: SQL error: [Microsoft] [ODBC driver manager] no data source name is found and no default driver is specified. SQL state IM002 in SQLConnect in E: \ wwwroot \ phperz.com \ phpweb \ conn. php on line 8
The most common cause of the error is that your connection string is wrong. The Complete connection string should be:
The code is as follows:
$ 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 ("failed to open the database! Contact the administrator ");
If your database does not have a password, Uid or Pwd can be omitted.
The code is as follows:
$ Connstr = "Driver = {Microsoft Access Driver (*. mdb)}; Dbq = $ db ";
$ Connid = odbc_connect ($ connstr, "", "", SQL _CUR_USE_ODBC) or die ("failed to open the database! Contact the administrator ");
Error 4:
Fatal error: Uncaught exception 'com _ exception' with message 'Source: ADODB. ConnectionDescription: no provider is found. The program may not be correctly installed. 'In ......................
Oledb connection mode. The cause of the error is the same as above. The Complete connection string should be:
The code is 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 );