Home  >  Article  >  Backend Development  >  PHP connection to Access database errors and solutions_PHP tutorial

PHP connection to Access database errors and solutions_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:04:52893browse

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

Copy the code 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";
?>


Part 2:

Copy code The code is as follows:

//Create 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 record set query
$rs = @new COM("ADODB.RecordSet");
$rs->Open("select * from blog_Content",$conn ,1,3);
echo $rs->Fields["log_Title"]->Value; //Output log_Title field
echo "
";
$rs- >Movenext(); //Move the record set pointer down
echo $rs->Fields["log_Title"]->Value;
$rs->close();
? >


The following is a supplement

1. Create an odbc driver and then use PHP’s odbc_connect() function to connect.

For example:

Copy code 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
, such as:

Copy code The code is as follows:

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

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

Copy code The code is as follows:

Warning: odbc_connect() [
function.odbc-connect
] : SQL error: [Microsoft][ODBC Microsoft Access Driver]Common error Cannot open registry key 'Temporary (volatile) Jet DSN for process 0xdd0 Thread 0xcb8 DBC 0x14bd024 Jet'. , SQL state S1000 in SQLConnect in E:wwwrootphperz.comphpwebconn.php on line 8

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:

Copy code The code is as follows:

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:wwwrootphperz.comphpwebconn.php on line 8


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:

Copy code 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("Database opening failed! Please contact the administrator");

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

Copy code The code is as follows:

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

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:

Copy code The code is as follows:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327736.htmlTechArticleThere are generally two common ways for php+access to connect to the database. In the recommended code, please note that the php acquisition path is used The realpath copy code is as follows: ?php $connstr="DRIVER={Microsoft Ac...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn