How to connect PHP to a Microsoft Access database

I am currently faced with a new problem for developing a site using Microsoft Access as a primary database instead of mysql. I have not used MS Access before, and I would like it to be done, I looked at the w3c website on W3schools but the code gives an error

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 C: \ Users \ NNALI \ Desktop \ root \ test.php on line 2

and this mistake

Warning: odbc_exec () expects parameter 1 to be a resource, boolean is set in C: \ Users \ NNALI \ Desktop \ Breweries \ root \ test.php on line 4

I am stuck and do not know what to do, I would be grateful for all the help in this.

<?php $conc = odbc_connect("northwind", "",""); $sql = "Select * From customers"; $rs = odbc_exec($conn, $sql); ?> 

Above is the code I used

+8
php ms-access odbc
source share
4 answers

If you are just starting out with a new project, I would suggest using PDO instead of the old odbc_exec() approach. Here is a simple example:

 <?php $bits = 8 * PHP_INT_SIZE; echo "(Info: This script is running as $bits-bit.)\r\n\r\n"; $connStr = 'odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};' . 'Dbq=C:\\Users\\Gord\\Desktop\\foo.accdb;'; $dbh = new PDO($connStr); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "SELECT AgentName FROM Agents " . "WHERE ID < ? AND AgentName <> ?"; $sth = $dbh->prepare($sql); // query parameter value(s) $params = array( 5, 'Homer' ); $sth->execute($params); while ($row = $sth->fetch()) { echo $row['AgentName'] . "\r\n"; } 
+6
source share
 <?php $dbName = $_SERVER["DOCUMENT_ROOT"] . "products\products.mdb"; if (!file_exists($dbName)) { die("Could not find database file."); } $db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;"); 

A successful connection will allow you to execute SQL commands from PHP to read or write the database. If, however, you receive a "PDOException Unable to Find Driver" error message, then it is likely that the PDO ODBC driver is not installed. Use the phpinfo () function to check the installation for links to PDOs.

If there is no entry for ODBC PDO, you need to make sure that your installation includes the PDO extension and ODBC drivers. To do this on Windows, uncomment the line extension = php_pdo_odbc.dll in php.ini, restart Apache and try connecting to the database again.

With the driver installed, the output from phpinfo () should contain the following information: https://www.diigo.com/item/image/5kc39/hdse

http://i.stack.imgur.com/Zwp2W.png

+3
source share

The problem is a simple typo. You specified your variable "conc" on line 2, but then you specified "conn" on line 4.

+3
source share

Are you sure the odbc connector is well made? if you do not repeat the step " Create an ODBC connection "

EDIT : Connection without DSN from php.net

// Microsoft Access

 $connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password); 

in your case, it could be if your file name is north wind and the mdb file extension:

 $connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=northwind", "", ""); 
+2
source share

All Articles