Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with a conditional statement

Status
Not open for further replies.

LTeeple

Programmer
Aug 21, 2002
362
0
0
CA
Hi PhPeople!

I am having a problem with a conditional statement - and am hoping a fresh set of eyes can point this out to me... I'm sure it's something stupid I've missed!

I am populating a table with results from a simple query,
if there are results I want to print them to screen (no problems here!) and if there are no results I want to print a message ("No data found: please try again"). The problem is I can't get the error message to display, even though I know I'm entering a query that wont return anything. Here's my code:

<snip>
$results = odbc_exec($connect,$query);
while (odbc_fetch_row($results)){
$lastname = odbc_result($results, 1);
$firstname = odbc_result($results, 2);
$address_line_1 = odbc_result($results, 3);
$address_line_2 = odbc_result($results, 4);
$city = odbc_result($results, 5);
$state_province = odbc_result($results, 6);
$zip_code = odbc_result($results, 7);
$phone_1 = odbc_result($results, 8);
if ($results){
print '<tr>
<td width=&quot;50%&quot; align=&quot;left&quot;><font face=&quot;Arial, Helvetica, Sans-serif&quot; size=&quot;2&quot;>';
echo &quot;<a href=\&quot;#\&quot; onClick=\&quot;javascript:eek:penListing('&quot;
.$lastname.&quot;','&quot;
.$firstname.&quot;','&quot;
.$address_line_1.&quot;','&quot;
.$address_line_2.&quot;','&quot;
.$city.&quot;','&quot;
.$state_province.&quot;','&quot;
.$zip_code.&quot;','&quot;
.$phone_1.&quot;')\&quot;>&quot;
.$lastname.&quot;</a></font></td>&quot;;
print '<td width=&quot;50%&quot; align=&quot;left&quot;><font face=&quot;Arial, Helvetica, Sans-serif&quot; size=&quot;2&quot;>'
.$firstname.'</font></td></tr>';
}
else{ //no results...
print '<font size=&quot;2&quot; face=&quot;Arial, Helvetica, sans-serif&quot;>
<strong>No records found. Try again</strong></font>';
exit;
} //close if results
} //close while loop
print '</table></form>';
odbc_close($connect);//close the connection
} //close if (submitted)
</snip>

Any help at all would of course be greatly appreciated!
[cheers] Cheers!
Laura
 

try

if (!$results){ //no results...
print '<font size=&quot;2&quot; face=&quot;Arial, Helvetica, sans-serif&quot;>
<strong>No records found. Try again</strong></font>';
exit;

}else{ //results
print '<tr>
<td width=&quot;50%&quot; align=&quot;left&quot;><font face=&quot;Arial, Helvetica, Sans-serif&quot; size=&quot;2&quot;>';
echo &quot;<a href=\&quot;#\&quot; onClick=\&quot;javascript:eek:penListing('&quot;
.$lastname.&quot;','&quot;
.$firstname.&quot;','&quot;
.$address_line_1.&quot;','&quot;
.$address_line_2.&quot;','&quot;
.$city.&quot;','&quot;
.$state_province.&quot;','&quot;
.$zip_code.&quot;','&quot;
.$phone_1.&quot;')\&quot;>&quot;
.$lastname.&quot;</a></font></td>&quot;;
print '<td width=&quot;50%&quot; align=&quot;left&quot;><font face=&quot;Arial, Helvetica, Sans-serif&quot; size=&quot;2&quot;>'
.$firstname.'</font></td></tr>';
}
} //close if results
Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
Hi, thanks for your reply, unfortunately, that doesn't work either. Bummer. [cheers]
Cheers!
Laura
 
I have few precision that may help (we never know)

You have the following line in your code :

$results = odbc_exec($connect,$query);

You need to know that $results while only contain 2 possible kind of values :

1 - FALSE : if any error occurs .

2 - Returns an ODBC result identifier if the SQL command was executed successfully.


In other words, if your SQL query is successfully executed, even if it doesn't return a single row, the value returned will be anything but FASLE. In fact, it will be a resource identifier.

You can use the function odbc_num_rows($results)

This function will return -1 in case of error. Otherwise, it will return the number of rows returned by your SQL query which can be 0, 1, 2 , ...


Keep me posted


ASiby
 
Hi asiby, thanks for your reply. Sadly it didn't fix my problem, here's my revised code:
Code:
<snip>
$results = odbc_exec($connect,$query);
while (odbc_fetch_row($results)){
  $lastname = odbc_result($results, 1);
  $firstname = odbc_result($results, 2);
  $address_line_1 = odbc_result($results, 3);
  $address_line_2 = odbc_result($results, 4);
  $city = odbc_result($results, 5);
  $state_province = odbc_result($results, 6);
  $zip_code = odbc_result($results, 7);
  $phone_1 = odbc_result($results, 8);
  if (odbc_num_rows($results) <= 0) {// no results or error
    print '
    <font size=&quot;2&quot; face=&quot;Arial, Helvetica, sans-serif&quot;>
    <strong>No records found. Try again</strong></font>';
    exit;
  }
  else {  
    print '
    <tr>
    <td width=&quot;50%&quot; align=&quot;left&quot;>
    <font face=&quot;Arial, Helvetica, Sans-serif&quot; size=&quot;2&quot;>';
    echo  &quot;<a href=\&quot;#\&quot; onClick=\&quot;javascript:openListing('&quot;
	  .$lastname.&quot;','&quot;
	  .$firstname.&quot;','&quot;
	  .$address_line_1.&quot;','&quot;
	  .$address_line_2.&quot;','&quot;
	  .$city.&quot;','&quot;
	  .$state_province.&quot;','&quot;
	  .$zip_code.&quot;','&quot;
	  .$phone_1.&quot;')\&quot;>&quot;
	  .$lastname.&quot;</a></font></td>&quot;;
    print '<td width=&quot;50%&quot; align=&quot;left&quot;>
    <font face=&quot;Arial, Helvetica, Sans-serif&quot; size=&quot;2&quot;>'
   .$firstname.'</font></td></tr>'; 
  }  //close if results
} //close while loop
print '</table></form>';
odbc_close($connect); //close the connection
</snip>

Any further ideas/advice would be greatly appreciated! [cheers]
Cheers!
Laura
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top