php connection to Access database error and resolution

  • 2020-06-15 07:56:10
  • OfStack

There are two common ways to connect php+access to a database.

Recommended code

Note that php USES realpath to get the path

 
<?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"; 
?> 


2:

 
<?PHP 
// create ADO The connection  
$conn = @new COM("ADODB.Connection") or die ("ADO The connection fails !"); 
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("temp/TempData.mdb"); 
$conn->Open($connstr); 

// Create a recordset query  
$rs = @new COM("ADODB.RecordSet"); 
$rs->Open("select * from blog_Content",$conn,1,3); 
echo $rs->Fields["log_Title"]->Value; // The output log_Title field  
echo "<br/>"; 
$rs->Movenext(); // Move the recordset pointer down  
echo $rs->Fields["log_Title"]->Value; 
$rs->close(); 
?> 


Here's the supplement

1. Set up odbc driver, and then connect with odbc_connect() function of php.

Such as:


$connstr=DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".$db;
$connid=odbc_connect($connstr,"username","password",SQL_CUR_USE_ODBC);

2. Connect with oledb and then call open to open
Such as:


$conn=new com("ADODB.connection");
$connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=".$db; 
$conn->Open($connstr);

But these 2 kinds of methods how connect all wrong, go looking for data on the net, some say that did not grant Everyone permission, some say that access97 and access2000 drive is not a kind (is the database is built in 2000, read with the driver is 97.) cause.

After repeated tests, it turned out to be caused by the path of the database. When developing asp, I used to write the address of the database as a relative path, and then used server.mappath () function to get its absolute path.

This habit was continued when php was developed, using realpath plus the relative path of the database to get the address of the database. For example :$db=realpath(".. / db mdb ");

However, asp's include function and php's include function seem to handle containing files in different ways, causing php to include conn.php file in different directories to connect to the database.

Or Uncaught exception 'com_exception' with message 'Source: ProviderDescription: Authentication failed. '.

Now put the error message and the solution to organize 1 post to everyone, I hope other friends do not encounter this situation as I am so depressed

Error 1

Common problems with CONNECTING to the access database


Warning: odbc_connect() [
function.odbc-connect
]: SQL error: [Microsoft][ODBC Microsoft Access Driver] Common mistakes   The registry key cannot be opened  '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

$connstr=DRIVER={Microsoft Access Driver (*.mdb)} DBQ = ". $db;

And the file you are currently accessing is not in the same directory as your ES98en.php database connection file, which contains the relative path used when conn.php (".. conn. php"), since php handles files in the include function differently than asp,

Caused an error in the database path,

Solutions:

1, check the path of your database, whether using realpath() function plus relative path to get,

Such as: $db = realpath (".. / db mdb ");
If so, use a different method to get the database address, such as $_SERVER['DOCUMENT_ROOT'] to get the root of your site, plus the database address

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

2. Check the permission and grant him Everyone permission

It is said that Microsoft itself has given up its support for the connection mode of odbc data source, and suggests users to use oledb mode. The connection string should be changed to the following mode:

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=".$db;

Mistake 2:

Fatal error: Uncaught exception 'com_exception' with 'message: ProviderDescription: Verification failed. 'in E:\wwwroot\ phpweb\conn :7 trace: #0 E \wwwroot\phperz \ php(7): com- > Open('Provider=Micros...') #1 ...............


This error indicates that you are using an oledb connection to the database and that the database path is the cause.

$_SERVER['DOCUMENT_ROOT']."\ db. mdb"; Method gets the database address

Mistake 3:


Warning: odbc_connect() [
function.odbc-connect
]: SQL error: [Microsoft][ODBC  Driver manager ]  No data source name was found and no default driver was specified , SQL state IM002 in SQLConnect in E:\wwwroot\phperz.com\phpweb\conn.php on line 8


For the database connected by odbc driver, the error is probably caused by your wrong connection string. The complete connection string should be:


$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 failed to open ! Please contact the administrator ");

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


$connstr="Driver={Microsoft Access Driver (*.mdb)};Dbq=$db";
$connid=odbc_connect($connstr,"","",SQL_CUR_USE_ODBC) or die(" Database failed to open ! Please contact the administrator ");

Mistake 4:
Fatal error: Uncaught exception 'com_exception' with message: ADODB: Provider not found. The program may not have been installed correctly. 'in...

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


$db=$_SERVER['DOCUMENT_ROOT']."\db.mdb";
$conn=new com("ADODB.connection");
$connstr="Provider=Microsoaft.Jet.OLEDB.4.0;Data Source=".$db;      
$conn->Open($connstr);


Related articles: