Make sure the selected array is empty or not PHP?

I am trying to check if mysql_fetch_array () returns an empty array or not. But my code does not seem to work. Here I want to make sure that if the array is empty, I want to display a construction message.

The code:

$queryContents= queryMembers(); $exeQuery = mysql_query($queryContents); while($fetchSet = mysql_fetch_array($exeQuery)) { if(count($fetchSet) == 0) { echo "This Page is Under Construction"; }else{ // something else to display the content } } 

How to check to perform such a function?

+4
source share
6 answers

use mysql_num_rows to count the number of rows. try it.

 $exeQuery = mysql_query($queryContents); if(mysql_num_rows($exeQuery)== 0){ echo "This Page is Under Construction"; } else{ while($fetchSet = mysql_fetch_array($exeQuery)) { // something else to display the content } } 
+13
source

You really have to use mysql_num_rows http://us2.php.net/manual/en/function.mysql-num-rows.php

However, on the side of the note, you should use php empty() . http://us2.php.net/empty

+5
source

When you use mysql_fetch_array (), it returns the rows from the data set one by one when you use the while loop.

If there is no record, while the loop will not be executed. In this case, declare the boolean variable and make it true if it enters the while loop. How:

 $queryContents= queryMembers(); $exeQuery = mysql_query($queryContents); $recordExists = 0; while($fetchSet = mysql_fetch_array($exeQuery)) { if($recordExists == 0 ) $recordExists = 1; // something else to display the content } if($recordExists == 0 ){ echo "This Page is Under Construction"; } 

Hope it works!

+4
source

You can do it as follows:

 while($r[]=mysql_fetch_array($sql)); // now $r has all the results if(empty($r)){ // do something } 

source: php doc

+1
source

try it

 if(empty($fetchSet) { echo "This Page is Under Construction"; } else { // something else to display the content } 
0
source

Your code inside the while loop never runs if there are no results. mysql_fetch_array returns null / false if there are no more results. What you need to do is first check with mysql_num_rows, before that.

 $queryContents= queryMembers(); $exeQuery = mysql_query($queryContents); if(mysql_num_rows ($exeQuery) == 0) { echo "This Page is Under Construction"; } while($fetchSet = mysql_fetch_array($exeQuery)) { // something else to display the content } 
0
source

All Articles