Click here to Skip to main content
15,795,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I connected and executed query without throwing any exception. But I tried fetching rows to display the data in a table and an exception was thrown at my foreach.
Here is my code:

$con = odbc_connect("My DSN","","");
$result = odbc_exec($con,"My query");
  echo "<table align=center>";
    echo "<tr>";
    foreach($row as $r)
       echo "<td>$r</td>";
    echo "</tr>";
echo "</table>";

It said that I used a variable ($row) which couldn't be iterated with foreach.
I have used the above code with mysql, mssql and they worked fine but odbc gave me that error, please remember that I connected and queried successfully, it reported the error line at the foreach statement as I said, it was so strange, and I have tried with odbc_result to fetch only 1 cell, it didn't throw exception but the result was empty.
Could you please give me some reason elaborating on this?
Any of your help would be appreciated!
Updated 29-Mar-12 19:02pm

1 solution

odbc_fetch_row returns a bool, which of course can't be iterated.
From the description in the manual:
bool odbc_fetch_row ( resource $result_id [, int $row_number ] )

Fetches a row of the data that was returned by odbc_do() or odbc_exec(). After odbc_fetch_row() is called, the fields of that row can be accessed with odbc_result().
Share this answer
supernorb 29-Mar-12 23:53pm    
Sorry, I have never tried refering how odbc_fetch_row is defined, looking at its name I guess it is the same to odbc_fetch_array, and in fact I coded using odbc_fetch_array, it returns an array, right? I'm sorry about my misunderstanding between those function, I'm editing my first post to change odbc_fetch_row to odbc_fetch_array, please review my problem.
Thank you for your help!
Peter_in_2780 30-Mar-12 0:28am    
Time for some debug statements. Put a line "var_dump($row)" right after the odbc_fetch_array line and see what it says. You can also look at odbc_errormsg() if something goes wrong.
supernorb 4-Apr-12 2:42am    
Thank you! I tried with var_dump, it said "bool(false)", even var_dump was placed in while loop, I can't understand why it couldn't be broken before var_dump was executed, (the condition values false). Do you have any idea?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900