contra10 Posted February 23, 2009 Share Posted February 23, 2009 im trying to get my else statement to work if there is nothing in the query <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); $queryi = "SELECT * FROM `users` WHERE `username` = '$usernamec'"; $resulti = mysql_query($queryi); if($comcount = mysql_num_rows($resulti)){ while($rowid = mysql_fetch_assoc($resulti)) { $com= "{$rowid['comment']}"; $cname= "{$rowid['cusername']}"; echo"$cname thinks $com"; } }else{ echo "no comments have been made"; } ?> theres nothing in my query so the second line should show but it shouws the first echo without the variables present Quote Link to comment Share on other sites More sharing options...
contra10 Posted February 23, 2009 Author Share Posted February 23, 2009 or how can i place the else statement so that if there is nothing to query it shows that comment Quote Link to comment Share on other sites More sharing options...
niranjnn01 Posted February 23, 2009 Share Posted February 23, 2009 check n see what the mysql_num_rows is returning... I guess in some cases, if there are no rows in the result set, mysql_num_rows will return 0 . this means that ur if will work. rewrite it as if(mysql_num_rows ($query ) ! = 0) Quote Link to comment Share on other sites More sharing options...
contra10 Posted February 23, 2009 Author Share Posted February 23, 2009 my statment shows but i get error with this <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); $queryi = "SELECT * FROM `users_should_comment` WHERE `username` = '$usernamec'"; $resulti = mysql_query($queryi); if($comcount = mysql_num_rows($resulti)){ while($rowid = mysql_fetch_assoc($resulti)) { $com= "{$rowid['comment']}"; $cname= "{$rowid['cusername']}"; echo"$cname thinks that $username should $com"; } }else{ echo "no comments have been made to this user"; } ?> Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\profile\index.php on line 205 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 23, 2009 Share Posted February 23, 2009 When you get that error it means your query has a problem. Rewrite your code to be something like this: <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); $queryi = "SELECT * FROM `users_should_comment` WHERE `username` = '$usernamec'"; $resulti = mysql_query($queryi) or die("Problem with the query: $queryi<br>" . mysql_error()); $comcount = mysql_num_rows($resulti); if ($comcount > 0) { while($rowid = mysql_fetch_assoc($resulti)) { $com= $rowid['comment']; $cname= $rowid['cusername']; echo "$cname thinks that $username should $com"; } }else{ echo "no comments have been made to this user"; } ?> Ken Quote Link to comment Share on other sites More sharing options...
contra10 Posted February 23, 2009 Author Share Posted February 23, 2009 your certainly right ken...and after searching and searching...i finally decided to start from the beginning and i found the problem...i was 1 letter of in my query...now i have a heache...lol thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.