This quick sample shows how to connect, retrieve records and pull error-information from any database using PHP and ODBC. More comprehensive examples are coming soon!
<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"); // these files contain IP address of Windows box
putenv("ODBCINI=/Library/ODBC/odbc.ini"); // hosting the ODBC Router
$weGotIt=1;
if ($_POST["go"] != "") {
$DBC = @odbc_connect("Northwind","",""); // "Northwind" is specified in the Windows ODBC control-panel
if (!$DBC) {
$errMsg = odbc_errormsg();
$weGotIt = 0;
}
else {
$resId = @odbc_exec($DBC, "SELECT * FROM Customers"); // run the query
if (!$resId) {
$errMsg = odbc_errormsg($DBC);
$weGotIt = 0;
}
else {
$resultCount = odbc_result_all( $resId ); // convenience ODBC func to display all results in HTML format
}
}
}
?>
<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>
<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"); // these files contain IP address of Windows box
putenv("ODBCINI=/Library/ODBC/odbc.ini"); // hosting the ODBC Router
$weGotIt=1;
if ($_POST["go"] != "") {
$DBC = @odbc_connect("Northwind","",""); // "Northwind" is specified in the Windows ODBC control-panel
if (!$DBC) {
$errMsg = odbc_errormsg();
$weGotIt = 0;
}
else {
$resId = @odbc_exec($DBC, "SELECT * FROM Customers"); // run the query
if (!$resId) {
$errMsg = odbc_errormsg($DBC);
$weGotIt = 0;
}
else {
$resultCount = odbc_result_all( $resId ); // convenience ODBC func to display all results in HTML format
}
}
}
?>
<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>