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
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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.