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 Link to comment https://forums.phpfreaks.com/topic/146443-can-get-else-statment-to-work/ 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 Link to comment https://forums.phpfreaks.com/topic/146443-can-get-else-statment-to-work/#findComment-768840 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) Link to comment https://forums.phpfreaks.com/topic/146443-can-get-else-statment-to-work/#findComment-768845 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 Link to comment https://forums.phpfreaks.com/topic/146443-can-get-else-statment-to-work/#findComment-768850 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 Link to comment https://forums.phpfreaks.com/topic/146443-can-get-else-statment-to-work/#findComment-768856 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 Link to comment https://forums.phpfreaks.com/topic/146443-can-get-else-statment-to-work/#findComment-768862 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.