David Egan
David Egan

Reputation: 434

odbc_exec() returns resource(3) of type (odbc result) in False situation php access

My code...

<?php

$parm1 = 8;

//Connect to our MS Access database
$conn=odbc_connect("archive","" ,"password");

//Create our SQL statement
$sql = "SELECT * FROM archive where archiveID = $parm1" . " order by fullname desc";

//Execute our SQL statement
$row=odbc_exec($conn, $sql);

var_dump($row);

 echo "<option value=''>Select Local Archive Location</option>";

     if ($row !== False) { //if there are records, process them.


             while(odbc_fetch_row($row)){ //Iterate through all our records.

                 echo   "<option value='" .trim(odbc_result($row,"archivelocation"))."'>".trim(odbc_result($row,"fullname")).' - '.trim(odbc_result($row,"archivelocation")).' - '.trim(odbc_result($row,"archivedescription"))."</option>";
             }  
     }else{
                 echo   "<option value=''>No data returned for the specified search criteria</option>"; 
     }


odbc_close($conn); //Close our database connection.


?>

When the above code runs and there is data to be returned the value of $row is resource(3) of type (odbc result). Which I understand to be correct. However when a False value is expected for $row I am still getting the same value resource(3) of type (odbc result) in a False situation.

Can anybody explain why this is?

Upvotes: 0

Views: 1890

Answers (2)

David Egan
David Egan

Reputation: 434

Now contrary to Tobias's answer above under testing I realised this was not working. In order to get the right answer I had to modify the code. The following answer works.

<?php

$parm1 = intval($_GET['q']);

//Connect to our MS Access database
$conn=odbc_connect("archive","" ,"password");

//Create our SQL statement
$sql = "SELECT * FROM archive where archiveID = $parm1" . " order by fullname desc";

//Execute our SQL statement
$row=odbc_exec($conn, $sql);

echo "<option value=''>Select Local Archive Location</option>";

     if (odbc_fetch_row($row)) { //Retrieve our first record, if any.

                do {

                    echo    "<option value='" .trim(odbc_result($row,"archivelocation"))."'>".trim(odbc_result($row,"fullname")).' - '.trim(odbc_result($row,"archivelocation")).' - '.trim(odbc_result($row,"archivedescription"))."</option>";

                } while (odbc_fetch_row($row));
     }else{
                    echo    "<option value=''>No data returned for the specified search criteria</option>"; 
     }


odbc_close($conn); //Close our database connection.


?>

Upvotes: 0

Tobias F.
Tobias F.

Reputation: 1048

From the PHP manual:

Returns an ODBC result identifier if the SQL command was executed successfully, or FALSE on error.

So you will always get an ODBC result identifier, except there was an actual error. Even when you have an empty resultset, $row will still be not equal to false. If you want to check wether there are any results, you should do it the following way:

if ($row !== false && odbc_num_rows($row) > 0) { //if there are records, process them.

  while(odbc_fetch_row($row)){ //Iterate through all our records.

    echo "<option value='" .trim(odbc_result($row,"archivelocation"))."'>".trim(odbc_result($row,"fullname")).' - '.trim(odbc_result($row,"archivelocation")).' - '.trim(odbc_result($row,"archivedescription"))."</option>";
  }
}else{
  echo   "<option value=''>No data returned for the specified search criteria</option>"; 
}

Upvotes: 1

Related Questions