Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php odbc_exec doesn't work unless I echo first?

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?

like image 301
solo1977 Avatar asked May 15 '26 06:05

solo1977


1 Answers

I set up a Virtual machine and reinstalled from scratch to see if the problem existed again on the new build. It didn't so it lead me to believe there was a problem with the build of Apache on the live machine. In the past few days I have removed and added Apache through the repositories and added all the latest updates through apt-get and the problem now appears to be resolved - very frustrated as the latest updates are normally applied so I can only imagine something must have gone wrong either with the initial install of apache2 or the php5-odbc.

like image 179
solo1977 Avatar answered May 17 '26 20:05

solo1977



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!