Php Access database errors and Solutions

Source: Internet
Author: User
Tags driver manager php database sql error

There are two common methods for connecting php + access to the database.

Recommended code

Note that php uses realpath to obtain the path.
Copy codeThe Code is as follows:
<? Php
$ 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:

Copy codeThe Code is as follows:
<? PHP
// 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 "<br/> ";
$ 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:
Copy codeThe 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:

Copy codeThe 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

Copy codeThe 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:

Copy codeThe 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:

Copy codeThe 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.

Copy codeThe 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:

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

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.