Conversion tables A-K
  Conversion tables L-Z
  SPAM database query
  Reverse WHOIS utility
  North America area codes
  PHP Example forums

PHP example repository.

Web www.web-max.ca
Back to examples
Using MS Access and ODBC in PHP ( part 1 )

For the sake of these examples, we will assume PHP is running on a Windows system. Using ODBC and Access databases is not the best way of handling large amounts of data or doing complex queries. However, in some enviroments where you cannot install MySQL or cannot afford a license for MSSQL, this will tend to the job.

Obviously the first thing we need to do is make a connection to the database. The easiest way to do this is to create a wrapper function.

The function odbc_connect_custom($db) assumes all your databases are located in the same place. To connect use $dblink = odbc_connect_custom($db) where $db is the filename of the mdb file ( minus the path and extension ) ie odbc_connect_custom("payroll") which will connect to "payroll.mdb" in whatever directory you specify in the function.

<?php

function odbc_connect_custom($db)
{

// $dbdir is the location of your databases.
// The main thing to watch out for is permissions.
// The process your web server is running under must
// have read / write permissions to this folder

    
$dbdir "D:\Inetpub\wwwroot\Databases\\";
    
    
$cfg_dsn "DRIVER=Microsoft Access Driver (*.mdb);
        DBQ="
.$dbdir.$db.".mdb;UserCommitSync=Yes;
        Threads=3;
        SafeTransactions=0;
        PageTimeout=5;
        MaxScanRows=8;
        MaxBufferSize=2048;
        DriverId=281;
        DefaultDir=C:/ProgramFiles/CommonFiles/ODBC/DataSources"
;

// The DefaultDir setting will probably be ok if you have gone for 
// a typical installation

    
$cfg_dsn_login "";
    
$cfg_dsn_mdp "";

    return 
odbc_connect($cfg_dsn,$cfg_dsn_login,$cfg_dsn_mdp);
}

?>
 
Example

<?php

    $db 
"monkeys";

    
$connection_id odbc_connect_custom($db);

    
$query      "SELECT * FROM monkey_types";
    
$result_id     odbc_exec($connection_id,$query);
    
?>