Connecting PHP to a Microsoft Access Database

My youngest son is learning how to code.

He is working on a PHP fitness application.

He’s built his demo app using PHP and MySQL and I want to run the code on my PC but I don’t have MySQL and don’t want to install it either.

Instead, I’ll try to do something much harder.

I’ll make his code connect to a Microsoft Access Database instead.

It was harder than I thought it would be initially.
I tried an ODBC connection but had to abandon it after having way too many issues running 32-bit ODBC drivers on 64 bit Windows that is trying to Microsoft Access Database files. 

To get this to work, I had to enable an ODBC dll in the PHP.ini file.  Then trial and error coding until I found some code that works but I ended up having to learn about PHP PDO and use that for connecting to the Access Database instead of a strait OBDC connection.

Configuring PHP to use a Microsoft Access Database

  1. Enable an ODBC.dll in the PHP.ini file
  2. Use code below to connect to the Microsoft Access Database using PHP PDO

PHP code I used to connect a PHP page to a Microsoft Access Database

<?php
try {
    $dbName = $_SERVER["DOCUMENT_ROOT"] . "phpFitness_Trackerlifting_tracker.mdb";
    if (!file_exists($dbName)) {
        die("Could not find database file." . $_SERVER["DOCUMENT_ROOT"] . "phpFitness_Trackerlifting_tracker.mdb" );
    }

    $db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");

    /*** The SQL SELECT statement ***/
    $sql = "SELECT * FROM lifting_tracker";
    /*** fetch into an PDOStatement object ***/
    $stmt = $db->query($sql);
    $stmt2 = $db->query($sql);
    /*** echo number of columns ***/
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    /*** loop over the object directly ***/
    foreach($result as $key=>$val) {
        echo $key.' - '.$val.'<br />';
        echo 'Reps: ' . $result['reps']. '<br />';
    }
    /*** loop over the object directly ***/
    while ($row = $stmt2->fetch(PDO::FETCH_ASSOC))
    {
      echo 'This is from the while loop! ' . $row['reps']. '<br />';
    }
}
catch(PDOException $e) {
    echo $e->getMessage();
}
$db = null;
?>

For Loops for Beginners – ASP, C#, PHP, JavaScript & Python Examples

A “For Loop” executes a block of code a specific number of times or while a specified condition is true.

PHP For Loop

for (init; condition; increment)
  {
  code to be executed;
  }

php fOR lOOP PARAMETERS

  • init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop)
  • condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  • increment: Mostly used to increment a counter (but can be any code to be executed at the end of the iteration)

Note: The init and increment parameters above can be empty or have multiple expressions (separated by commas). Example The example below defines a loop that starts with i=1. The loop will continue to run as long as the variable i is less than, or equal to 5. The variable i will increase by 1 each time the loop runs:

PHP For Loop Example Code

<?php
for ($i=1; $i<=5; $i++)
  {
  echo("The number is " . $i . "<br>");
  }
?>

Classic ASP For Loop Example Code

<%
For i = 1 to 5
 Response.Write("The number is " & i & "<br>")
Next
%>

JavaScript For Loop Example

<%
For i = 1 to 5
 Response.Write("The number is " & i & "<br>")
Next
%>

C# For Loop Example

for (int i = 0; i < 5; i++) 
      {
        Console.WriteLine(i);
      }    

Python For Loop Example

states = ["Alaska", "Alabama", "Arkansas"]
for x in states:
  print(x) 
  if x == "Alabama":
    break