title: Using PHP to query a MDB file, and return JSON
body: <p>I have a Microsoft Access Database, and I am trying to query the table using PHP, and output valid JSON. I have an equivalent code for a MSSQL database, am I am trying to make my code do the same thing, but just for the Access database.</p>
  
  <p>Here is the MSSQL code</p>
  
  <pre><code>$myServer = "server";
  $myDB = "db";
  $conn = sqlsrv_connect ($myServer, array('Database'=&gt;$myDB));
  
  $sql = "SELECT *
          FROM db.dbo.table";
  
  $data = sqlsrv_query ($conn, $sql);
  
  $result = array();   
  
  do {
      while ($row = sqlsrv_fetch_array ($data, SQLSRV_FETCH_ASSOC)) {
          $result[] = $row;   
      }
  } while (sqlsrv_next_result($data));
  
  $json = json_encode ($result);
  
  sqlsrv_free_stmt ($data);
  sqlsrv_close ($conn); 
  </code></pre>
  
  <p>Here is what I tried for the MDB file</p>
  
  <pre><code>$dbName = "/filename.mdb";
  
  if (!file_exists($dbName)) {
      die("Could not find database file.");
  }
  
  $db = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", $user, $password);
  
  $sql = "SELECT *
          FROM cemetery";
  
  $data = $db-&gt;query($sql); // I'm getting an error here
  $result = array();   
  
  // Not sure what do do for this part...
  do {
      while ($row = fetch($data, SQLSRV_FETCH_ASSOC)) {
          $result[] = $row;   
      }
  } while (sqlsrv_next_result($data));
  
  $json = json_encode ($result);
  </code></pre>
  
  <p>I kind of followed this to try to connect to the database: <a href="http://phpmaster.com/using-an-access-database-with-php/" rel="nofollow">http://phpmaster.com/using-an-access-database-with-php/</a></p>
  
  <p>Currently this is giving me a 500 Internal Server Error. I'm expecting a string such as this to be saved in the variable <code>$json</code></p>
  
  <pre><code>[
      {
          "col1":"col value",
          "col2":"col value",
          "col3":"col value",
      },
      {
          "col1":"col value",
          "col2":"col value",
          "col3":"col value",
      },
      {
          etc...
      }
  ]
  </code></pre>
  
  <p>Can someone help me port the MSSQL code I have above so I can use it with an MDB database? Thanks for the help!</p>
  
  <hr>
  
  <p>EDIT: I'm commenting out the lines one by one, and it throws me the 500 error at the line <code>$data = $db-&gt;query($sql);</code>. I looked in the error log, and I'm getting the error <code>Call to a member function query() on a non-object</code>. I already have the line <code>extension=php_pdo_odbc.dll</code> uncommented in my php.ini file. Anyone know what the problem could be?</p>