Looking for assistance in, as the title reads, accessing an Access database via PHP when the database is open. (Please bear with me as my knowledge of PHP, when used in conjunction with a database, is extremely sketchy at best.)

Using the code below, I can pull up a directory of employees. It's nothing fancy but it works and I'm still learning:

$conn=odbc_connect('Employees','','');
if (!$conn)
  {exit("Connection Failed: " . $conn);}

$sql="SELECT * FROM tblEmployees WHERE CompanyID = '$comp' AND Active = Yes AND Generic = No ORDER BY LastName";
$rs=odbc_exec($conn,$sql);
if (!$rs)
  {exit("Error in SQL");}
echo "<table>";
echo "<tr><th>Name</th>";
echo "<th>Department</th>";
echo "<th>Email Address</th></tr>";
while (odbc_fetch_row($rs))
  {
  $compdept=odbc_result($rs,"Dept");
  $empfirst=odbc_result($rs,"FirstName");
  $emplast=odbc_result($rs,"LastName");
  $empemail=odbc_result($rs,"ECAddress1");
  echo "<tr><td>$emplast, $empfirst</td>";
  echo "<td>$compdept</td>";
  echo "<td>$empemail</td></tr>";
  }
odbc_close($conn);
echo "</table>";

?>

The problem is that when someone has the database open on their computer, the connection fails.

There are a number of possible reasons that come to mind, but I was hoping that someone would be able to point out something I'm missing.

Eric

Member Avatar for LastMitch

You connection should look like this:

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

You can read more here about Access Database with PHP:

http://phpmaster.com/using-an-access-database-with-php/

The problem is that when someone has the database open on their computer, the connection fails.

I think you have to give permission on that computer in order for the database to open on that computer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.