I am trying to query an access MDB file using php odbc_connect and odbc_exec. The idea is to return an array which I can then translate to json.
I am running php through apache2 on Ubuntu 12.10
Here is my code:
//Connect to the database
$conn=odbc_connect('stock-test','','');
//die if error
if (!$conn) {
die("Connection Failed: " . $conn);
}
//SQL query
$sql = "SELECT * FROM Stk_Items";
//This is the print command...see notes below for this
//print " "
//Execute SQL query
$rs=odbc_exec($conn,$sql);
//If no result, there is an error in the SQL
if (!$rs) {
exit("Error in SQL");
}
//Create an array to contain the results...
$arr = array();
//Loop through the results, pushing each array to the $arr array
while ($row = odbc_fetch_array($rs)) {
array_push($arr, $row);
}
print json_encode( $arr);
odbc_close($conn);
Now here is the strange thing. This code will only output the json if I print a blank space (or any other character) before the odbc_exec command is issued (I have commented out the command in the code above)
I have also run several other tests (echoing the results etc) but none will work unless I print some blank space before the odbc_exec command.
Am I missing something obvious?