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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

echo reveals 1, but follows if statement for 0?

Status
Not open for further replies.

AimeeAult

Programmer
Dec 22, 2004
3
US
I have a database search. $search comes from post data, as does $type. Whenever I have it echo the number after it does the query, it returns 1, but for some reason it tells me I have no matches. Does anyone have any ideas on what may be going on with this?

if ($type == "E-mail Address"){
$sql = "SELECT * FROM user_driver WHERE driver_email = '$search'";
$result = mysql_query($sql) or die ("#1 err.");
$num = mysql_num_rows($result);
echo($num);
  if ($num == 0) // no matches
     {
     echo ("Sorry, no matches!");
     }
  elseif ($num > 0) // login name not found
    {
    unset($do);
    $message = "Found matches";
    include ("search_form.inc");
    }
  }

Thanks in advance,
Aimee
 

if ($type == "E-mail Address"){
$sql = "SELECT * FROM user_driver WHERE driver_email = '$search'";
$result = mysql_query($sql) or die ("#1 err.");
$num = mysql_num_rows($result);
echo($num);
if ($num == 0) // no matches
{
echo ("Sorry, no matches!");
}
elseif ($num > 0) // login name not found
{
unset($do);
$message = "Found matches";
include ("search_form.inc");
}
}

Sorry about the nbsps :)
 
FYI, you can wrap your code segments in
Code:
Tags to have it do the formatting properly for you.

Outside of that... no, I can't imagine why it would echo 1, and then echo Sorry, no matches!

Are you sure something else isn't echo'ing 1?

I like to do
echo "!!!!!".$num."!!!!!";

to make sure I'm not reading a value printed out somewhere else.

If it is indeed still behaving as you say, all I can suggest is to try a debugger (i.e. Zend Studio)
 
for my tired eyes, your code looks o.k.
try this:

Code:
if ($num < 1)  // no matches
   {
    echo ("Sorry, no matches!");
   }
else   // login name not found         
   {
   unset($do);                                      
   $message = "Found matches";
   include("search_form.inc");
   }

Olav Alexander Mjelde
Admin & Webmaster
 
Yeah, I tried that. That worked fine for some reason. I've been having weird issues like this lately on my server. Same thing happened with some code for a session variable. Code was fine, didn't work. Oh well.

Thanks for the input :)

Aimee
 
I think that its some logical error.

If you check for only two values, you can check like I did.

You can also use switch, as it's more structured and easier to read, when debugging.

good luck!

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top