4

I am trying to check if the mysql_fetch_array() function returns an empty array or not. But my code doesn't seem to work. Here I want to ensure that if the array is empty I want to display under construction message.

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 do I check to acheive such feature ?

1
  • What is in $fetchSet? (try a var_dump() and inspect) Commented Aug 3, 2012 at 6:50

6 Answers 6

14

use mysql_num_rows to count number of rows. try this.

$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

   }
}
Sign up to request clarification or add additional context in comments.

Comments

5

You really should be using mysql_num_rows https://www.php.net/manual/en/function.mysql-num-rows.php

However, on a side note, you should use php empty() instead. https://www.php.net/empty

Comments

4

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

If there will be no record, while loop wont execute. In this case, declare a boolean variable and make it true if it enters the while loop. Like:

$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 this works!

3 Comments

what is the point of running the loop if there are no results?
You are absolutely right. This was just another implementation. However, yours good! or to be specific, your;s right!
I am not trying to say my answers is the only way of doing this. here if the number of rows are 0 there is no need of going to a while loop. so it is more efficient to have inside else part.
1

You can do it this way:

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

source: php doc

Comments

0

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 yo do is check with mysql_num_rows first, before the while.

$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
}

1 Comment

what is the point of running the loop if there are no results?
0

Try this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.