solo1977
solo1977

Reputation: 393

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?

Upvotes: 3

Views: 1880

Answers (2)

solo1977
solo1977

Reputation: 393

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.

Upvotes: 2

Gord Thompson
Gord Thompson

Reputation: 123399

What you describe sounds like an apache quirk to me. If you are using a web browser to trigger the PHP script and inspect the results then you really should be outputting at least <html><body> before anything else. If you want to see the results in a reasonably unmolested form you should echo a <pre> tag, too.

FWIW, I ran your code as-is from the command line and it worked fine for me.

Edit:

Further to comments below, this is indeed looking like an oddity when apache2 invokes a PHP script that uses odbc_exec(). To test, I created the following "page" on my Ubuntu 12.04 LAMP server...

<?php
$arr = array(
    array('ID' => 1, 'text' => 'foo'), 
    array('ID' => 2, 'text' => 'bar'), 
    array('ID' => 3, 'text' => 'baz')
    );
print json_encode($arr);

...and Firefox does display the JSON string as follows...

[{"ID":1,"text":"foo"},{"ID":2,"text":"bar"},{"ID":3,"text":"baz"}]

...so apache2 has no problem spitting out a plain JSON string and Firefox has no problem displaying it.

Upvotes: 0

Related Questions