Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to Access 1997 with PHP. It's possible?

I'm trying to create a simple connection to Microsoft Access DB (1997) using the PDO function on localhost with WampServer Version 2.5. After running the code below I get the error message like this:

Database file findSQLSTATE[HY000] SQLDriverConnect: -1019 [Microsoft][Pilote ODBC Microsoft Access] Can not open a database created with an earlier version of your application.

(Fatal error: Call to a member function query() on a non-object in C:\wamp\www\test2\index.php on line 23)

Can I work with MS Access date from 1997? And how can I solve this error message?

my file pdo.php

<?php    
try {

      function pdo($dbName) {
          return new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$dbName; Uid=admin; Pwd=''");
          }

        //Chemin vers le fichier .mdb
      	$dbName = "C:\\Users\\c.bolteau\\Desktop\\MAB\\BD\\C_Compta.mdb";

      		  if (!file_exists($dbName)) {
                  // Si le fichier n'existe pas
                  echo ("Could not find database file.");

              } else {

                  // Si le chemin spécifié est un fichier
                  // On essaie de s'y connecter
                  echo ("Database file find");
                  $pdo = pdo($dbName);
                }
        }
      	
     // en cas des erreurs on arrête en affichant un message Erreur 
     catch (PDOException $e)
      {
        echo $e -> getMessage();
      	// die('Erreur :'. $e->getMessage());
      }

    ?>

and my file index.php

<?php 
include "./pdo.php";

global $pdo;

 $query = "SELECT ET_CODE FROM LIGNE_TYPE";
 $values = $query->fetch();
 print_r($values);
 
 unset($pdo);
?>

Thank's everyone for your answers! Have a nice day!

like image 208
Max.Bob Avatar asked Dec 06 '25 07:12

Max.Bob


1 Answers

In this case you have to use odbc. Check this to know how to use pdo and odbc. Then check this to know how to create odbc link to your database file.

like image 121
SaidbakR Avatar answered Dec 08 '25 20:12

SaidbakR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!