mattVo Posted June 4, 2009 Share Posted June 4, 2009 Hi all, This is probably really easy, but I was wondering how to check if a query result exists. If it does then echo it, if not, check again until the query results are returned. I'm asking this because my pages get frequently displayed even when no query results exist, resulting in a page without content. THanks all, Matt Quote Link to comment https://forums.phpfreaks.com/topic/160963-check-to-see-if-query-result-exists/ Share on other sites More sharing options...
Ken2k7 Posted June 4, 2009 Share Posted June 4, 2009 var_dump(mysql_num_rows($result)); If that returns an int > 0, then you got results. Quote Link to comment https://forums.phpfreaks.com/topic/160963-check-to-see-if-query-result-exists/#findComment-849446 Share on other sites More sharing options...
gevans Posted June 4, 2009 Share Posted June 4, 2009 <?php /* * db connection omitted * all error checking ommited * basicaly you only have what you asked for */ $sql = "SELECT * FROM `somewhere`"; $result = mysql_query($sql); if(mysql_num_rows($result) > 0) { //you have results } else { //you don't have results } Quote Link to comment https://forums.phpfreaks.com/topic/160963-check-to-see-if-query-result-exists/#findComment-849448 Share on other sites More sharing options...
mattVo Posted June 4, 2009 Author Share Posted June 4, 2009 Thanks for the quick replies, But I would like to check until I have results, sort of like going through a loop to check. If none exist, go through the loop until I have results, then do something with them. Matt Quote Link to comment https://forums.phpfreaks.com/topic/160963-check-to-see-if-query-result-exists/#findComment-849461 Share on other sites More sharing options...
Alex Posted June 4, 2009 Share Posted June 4, 2009 Depending on your situation it's sometimes useful just to make a function like user_exists(), you could input a username and it would return true or false, the core of that function could look like: $result = create_query('some query..'); if(!mysql_num_rows($result)) { return false; } return true; Quote Link to comment https://forums.phpfreaks.com/topic/160963-check-to-see-if-query-result-exists/#findComment-849463 Share on other sites More sharing options...
Ken2k7 Posted June 4, 2009 Share Posted June 4, 2009 Thanks for the quick replies, But I would like to check until I have results, sort of like going through a loop to check. If none exist, go through the loop until I have results, then do something with them. Matt What do you mean? If a SQL returns no results, then looping it won't do any good. Mind posting up the pertinent code so we may know what you're talking about? Quote Link to comment https://forums.phpfreaks.com/topic/160963-check-to-see-if-query-result-exists/#findComment-849475 Share on other sites More sharing options...
mattVo Posted June 4, 2009 Author Share Posted June 4, 2009 Hope this makes things clearer: Check to see if SQL results exist, if not, check again and again until they exist. When they do exist, use them. Quote Link to comment https://forums.phpfreaks.com/topic/160963-check-to-see-if-query-result-exists/#findComment-849601 Share on other sites More sharing options...
Alex Posted June 4, 2009 Share Posted June 4, 2009 Hope this makes things clearer: Check to see if SQL results exist, if not, check again and again until they exist. When they do exist, use them. What do you mean check-again? The results aren't going to change within the execution of the script... Quote Link to comment https://forums.phpfreaks.com/topic/160963-check-to-see-if-query-result-exists/#findComment-849603 Share on other sites More sharing options...
mattVo Posted June 4, 2009 Author Share Posted June 4, 2009 The problem I am having is that my pages get displayed before SQL query results get in. So I would like to check when they get in, to then display the page. That's why I'd like to check if results exist. Quote Link to comment https://forums.phpfreaks.com/topic/160963-check-to-see-if-query-result-exists/#findComment-849605 Share on other sites More sharing options...
BobcatM Posted June 4, 2009 Share Posted June 4, 2009 Post your code. Quote Link to comment https://forums.phpfreaks.com/topic/160963-check-to-see-if-query-result-exists/#findComment-849608 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.