...

View Full Version : if else statement not working



harkly
06-29-2010, 07:01 PM
I am running this query and I need it to print out the "Nothing Found" when nothing is found but not sure where to put that else statement.

I have tried to put it in both if's but does not work. Clearly I am not understanding how this works. CAn someone help me??


$query = ("SELECT user.userID, user.gender, user.bd_year, user.city, user.state, photos.userID, photos.photo_1, CURDATE(), (YEAR(CURDATE())-YEAR(birth_date)) - (RIGHT(CURDATE(),5)<RIGHT(birth_date,5)) AS age
FROM user LEFT JOIN photos ON user.userID = photos.userID
WHERE user.bd_year <= $year1 AND user.bd_year >= $year2 AND user.gender = $genderPref ");

if ($result = mysql_query($query))
{
echo " Hmm ";

$c = 0;
if ($num_rows != '0')
{
echo " Do this stuff ";
}
}
else
{
echo " Nothing found ";
}

Right now it echos both the "1st if statement" & "2nd if statement" regardless of the search criteria.

Fou-Lu
06-29-2010, 07:18 PM
The statement indicates that your query itself has failed. The else branch is associated with the mysql_query, and probably shouldn't be indicated as a 'Nothing Found' answer.


if ($result = @mysql_query($query))
{
echo 'hmmm';
$c = 0;
if (mysql_num_rows($result) === 0)
{
echo 'There are no results found.';
}
else
{
echo 'Results has been found';
while ($row = mysql_fetch_assoc($result))
{
// Do something with $row.
}
}
}
else
{
die('MySQL query failed: ' . mysql_error());
}

Of course you can capture any variables you want. Key things to remember are that the result of a mysql_query is a resource, not a collection of immediatly usable data. To make use of it, you need to use mysql_fetch_assoc, mysql_fetch_array, mysql_fetch_row, mysql_result, or evaluate other data with operations like mysql_num_rows.


Sorry, your wanting it to echo nothing found, not that it is. My bad, the query itself looks like it successfully executed, so use the above to determine if nothing is found.

Beagle
06-29-2010, 07:43 PM
mysql_query returns FALSE on error ONLY, otherwise it returns something that evaluates to true.

If your query executes successfully but pulls 0 rows, mysql_query will return something that evaluates to true.

So your code should really be:



if ($result = mysql_query($query))
{
// success
if (mysql_num_rows($result))
{
// query returned data
}
else
{
// query did not return data
}
}
else
{
// handle error
}

harkly
06-29-2010, 07:49 PM
Thanks!

I switched this line


if ($num_rows != '0')

to this and it works


if (mysql_num_rows($result))

technolojik
06-29-2010, 09:25 PM
you can use this


$query = ("SELECT user.userID, user.gender, user.bd_year, user.city, user.state, photos.userID, photos.photo_1, CURDATE(), (YEAR(CURDATE())-YEAR(birth_date)) - (RIGHT(CURDATE(),5)<RIGHT(birth_date,5)) AS age
FROM user LEFT JOIN photos ON user.userID = photos.userID
WHERE user.bd_year <= $year1 AND user.bd_year >= $year2 AND user.gender = $genderPref ");

if ($result = mysql_query($query))
{
echo " Hmm ";

$c = 0;
if (mysql_num_rows($result))
{
echo " Do this stuff ";
}
}
else
{
echo " Nothing found ";
}

harkly
06-29-2010, 09:26 PM
Thanks!!

Got it working!!

Fou-Lu
06-29-2010, 10:26 PM
you can use this



$query = ("SELECT user.userID, user.gender, user.bd_year, user.city, user.state, photos.userID, photos.photo_1, CURDATE(), (YEAR(CURDATE())-YEAR(birth_date)) - (RIGHT(CURDATE(),5)<RIGHT(birth_date,5)) AS age
FROM user LEFT JOIN photos ON user.userID = photos.userID
WHERE user.bd_year <= $year1 AND user.bd_year >= $year2 AND user.gender = $genderPref ");

if ($result = mysql_query($query))
{
echo " Hmm ";

$c = 0;
if (mysql_num_rows($result))
{
echo " Do this stuff ";
}
}
else
{
echo " Nothing found ";
}




Ummm, no. This still suffers from a deficiency of not being able to detect a failure versus the number of items being less than or equal to 0. The only time nothing will be found is when there is an error in the SQL itself.

You can however move the else to branch from the inner if instead of the outter if (as I've shown in post #2).

harkly
06-30-2010, 01:57 AM
I know, I stated above that I had been moving the else statement around because I couldn't get it to work. My main problem was the "if (mysql_num_rows($result)) " line.

With that corrected I was able to get it to work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum