j33baS Posted March 10, 2008 Share Posted March 10, 2008 Hi I've spent ages googling this but cant find anything which helps so naturally I came here ! so let me explain ... I have a player profile page for a basketball site which will display the types of points scored by a player. Lets say I have two tables, one holding info(player name, number scored and opposition) on 3pointers and one for 2pointers 3 pointers jack - 5 vs reds jack - 3 vs blues jack - 4 vs greens Gill - 6 vs reds Gill - 4 vs blues 2 pointers Gill - 9 vs reds Gill - 8 vs blues The default profile page will have to contain queries for both the 2pointers and 3pointers tables. But Because jack hasn't scored and 2 pointers, his profile page will display the 3pointers he has scored, but the 2pointers bit will just be blank, I'd like it to say sumthing like "no 2 pointers scored yet" So im basically my code says... $sql "SELECT * FROM 2points WHERE id = $id"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)){ echo "...output scores, names etc"; } and the same goes for the 3pointers table .... but what im trying to do is more along the lines of ... if (this query exists){ $sql "SELECT * FROM 2points WHERE id = $id"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result)){ ...output scores, names etc } } else{ echo "nothing scored yet"; } can anyone help me here? the codes not the problem because im getting the ouputs i want, its just the error handling, so I want "nothing scored yet" if the query is going to be empty cheers! Link to comment https://forums.phpfreaks.com/topic/95504-a-check-to-see-if-a-query-will-return-anything/ Share on other sites More sharing options...
frijole Posted March 10, 2008 Share Posted March 10, 2008 what about this? if (this query exists){ $sql "SELECT * FROM 2points WHERE id = $id"; $result = mysql_query($sql); $rows = mysql_num_rows($result) if ($rows == 0) { echo "nothing scored yet"; } else { while ($row = mysql_fetch_array($result)){ ...output scores, names etc } } } Link to comment https://forums.phpfreaks.com/topic/95504-a-check-to-see-if-a-query-will-return-anything/#findComment-488860 Share on other sites More sharing options...
j33baS Posted March 10, 2008 Author Share Posted March 10, 2008 hey, thanks for helping! I already tried that ... Im getting an error sayig that theres an "Unexpected T_ELSE" but thats the path i was wanting to go down! Any reasons as to why it might be producing that error ? its not a problem having a while loop nested within the if ? cheers Link to comment https://forums.phpfreaks.com/topic/95504-a-check-to-see-if-a-query-will-return-anything/#findComment-488934 Share on other sites More sharing options...
BlueSkyIS Posted March 10, 2008 Share Posted March 10, 2008 sounds like your brackets aren't matching or you're missing a semi-colon. can you post the latest code? Link to comment https://forums.phpfreaks.com/topic/95504-a-check-to-see-if-a-query-will-return-anything/#findComment-488942 Share on other sites More sharing options...
j33baS Posted March 10, 2008 Author Share Posted March 10, 2008 $sql "SELECT * FROM 2points WHERE id = $id"; $result = mysql_query($sql); $row = mysql_num_rows($result); if($row == 0){ echo "nothing scored"; } else{ while ($row = mysql_fetch_array($result)){ echo "scores names etc"; } } ok sorry, i did miss a bracket! codes running fine now, except im not getting the output i want ! "Jacks" 2point query still displays nothing and not the text "nothing scored" ! and Gills querys both display the info fine. Thanks for the help guys, any more suggestions ? Link to comment https://forums.phpfreaks.com/topic/95504-a-check-to-see-if-a-query-will-return-anything/#findComment-488980 Share on other sites More sharing options...
darkfreaks Posted March 10, 2008 Share Posted March 10, 2008 <?php $sql "SELECT * FROM 2points WHERE id = $id"; $result = mysql_query($sql); if(empty($result)||$result==""){die('there was an error with the query!');} $row = mysql_num_rows($result); if($row =="0"){ echo "nothing scored"; } else{ while ($row = mysql_fetch_array($result)){ echo "scores names etc"; } } ?> Link to comment https://forums.phpfreaks.com/topic/95504-a-check-to-see-if-a-query-will-return-anything/#findComment-488986 Share on other sites More sharing options...
j33baS Posted March 11, 2008 Author Share Posted March 11, 2008 awesome cheers guys! ...but wheres the "topic solved" mod gone ? Link to comment https://forums.phpfreaks.com/topic/95504-a-check-to-see-if-a-query-will-return-anything/#findComment-489047 Share on other sites More sharing options...
darkfreaks Posted March 11, 2008 Share Posted March 11, 2008 has not been fixed yet part of the update gone bad Link to comment https://forums.phpfreaks.com/topic/95504-a-check-to-see-if-a-query-will-return-anything/#findComment-489055 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.