Jump to content

MySql PHP IF ELSE not working


smidgen11

Recommended Posts

I know this is probably something very dumb but I am having trouble with my if else. I have a sql query that looks for a record and if nothing is found it echos a warning.

 

<?php

$sql7="SELECT * FROM `$tbl_name2` WHERE Mac = '$Mac' AND Type = '$Type'"; 
$result7=mysql_query($sql7);

  if($result7)
{
while($row=mysql_fetch_array($result7))
echo
"The phone you are using with mac $row[0] $row[1] is a Cisco IP Phone! <br>" ;
}
else 
{
echo "<img src=\"stop.png\"/> The phone you are trying to use with mac $row[0] $row[1] is not a correct Mac\Type. <br> Please confirm the mac and type again.";
}

?>

 

 

 

I believe my syntax is correct as my if echo works fine. However, when no records are found I do not see my last else echo. Any input?

Link to comment
https://forums.phpfreaks.com/topic/233514-mysql-php-if-else-not-working/
Share on other sites

You're not checking for the amount of results.  Change this line to:

  if(mysql_num_rows($result7) > 0)

 

Look up the returned values for mysql_query.  The only time you will get to your else is if your query returns a FALSE on error (and maybe some other cases).

A query that executes without any errors, returns a result resource. That result resource may contain zero rows. You would use mysql_num_rows to test how many rows were matched.

 

A query that results in an error will return a FALSE value. Your code is only testing if the query executed with or without an error.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.