This code shows how to connect, retrieve records and pull error-information from any database using PHP and ODBC.

<html>
<head><title>Northwind Test</title></head>
<body>

<?php
// Next two lines are for Mac OS X and Mac OS X Server (XServe) only:
putenv("ODBCINSTINI=/Library/ODBC/odbcinst.ini");
putenv("ODBCINI=/Library/ODBC/odbc.ini");

$weGotIt=1;
if ($_POST["go"] != "") {
    $DBC = @odbc_connect("Northwind","","");
    if (!$DBC) {
         $errMsg = odbc_errormsg();
         $weGotIt = 0;
    }
    else {
         $resId = @odbc_exec($DBC, "SELECT * FROM Customers");
         if (!$resId) {
            $errMsg = odbc_errormsg($DBC);
            $weGotIt = 0;
         }
         else {
            $resultCount = odbc_result_all( $resId );
         }
    }
}
?>

<form name="northwind" method="post">
      Ready? Set.. <input name="go" value="Go!" type="submit">
</form>

<?php
        if (!$weGotIt) {
?>
<p> %ODBC failure: [<?php echo $errMsg; ?>]</p>
<?php
        }
        echo "Total of $resultCount records.";
?>
</body>
</html>

Next: PHP and ODBC

© August Software, USA., 2009. All Rights Reserved.
Page was generated in 0.0655 seconds