2

Explanation & Problem

Okay we have the following columns in our table,

[username] - [message] - [status] - [id]

In some specify PHP page, we use a while loop to post ALL of the rows that are stored in the database.

Okay, but now I want to check if there are any rows in the that table, if not, echo an error.

It's okay, simple as this:

$check = $fetch->rowCount();

and then

if ($check == 0) { }

But now, I want to check if column 'Status' is 0.

So if there are no rows with columns 'status' with value 0, it will echo a message. If there are no rows at all, it will echo a message.

If there are rows with column 'status' with value 0, it will echo all rows.

But there are some problems atleast for me, doing so.

Problem

I can't do all of the IF statements checks inside a loop, because a loop can't run if there are no rows.

But okay, we can do the check if there are rows outside of the loop, but how would I check if there are rows + they are not with status column holding value higher than '0'?

I got it to work somehow, but not like I wanted..

If there are rows with column 'status' with value 0 + rows with column status with value higher than 0 at same time, it will echo the message + show the rows, which shouldn't work like this!

This is the code I've tried to build:

        $check = $fetch->rowCount();
        if ($check == 0) {
            echo '<br /> EMPTY';
        }
        while($row = $fetch->fetch( PDO::FETCH_ASSOC )) {
            if ($check > 0 && $row['status'] == 0) {
                echo 
                '<div class="background">'.$row['email'].' <span style="margin-left: 10px;"><font color="#ff0000">UNANSWERED</font></span><span id="right"><a href="index.php?recover='.$row['recover_id'].'">Read More</a></span></div><br />
                ';
            } else {
                echo 'There are no rows!';
            }
        }

Thank you for your help!

1
  • You are checking if $check == 0 but still run the while loop afterwards. You should put an else before the while. Commented Apr 1, 2013 at 2:05

2 Answers 2

1

But now, I want to check if column 'Status' is 0. So if there are no rows with columns 'status' with value 0, it will echo a message. If there are no rows at all, it will echo a message. If there are rows with column 'status' with value 0, it will echo all rows.

Why dont you send that condition to database in your WHERE clause

SELECT username,message,status,id FROM yourTABLE where status=0

So when there are such rows, it will display them. If there are no such rows then you will get your rowcount as 0 and then you can echo the message accordingly

Tip

Fetching all rows from a table and then bringing those to PHP only to discard most of them is just like going to a fruit shop buying all the available fruits, bringing them home and then saying Uh no i don't need Oranges,Apples,Apricot etc etc etc. You have to send conditions to your Database, and not bring all data without any condition and then put conditions in PHP.

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

1 Comment

Wow.... I haven't thought of this.. So easy fix.. Thanks a lot mate! I gotta use the WHERE more often now.. so useful.
1

try this:

$check = $fetch->rowCount();
if ($check == 0) {
    echo '<br /> EMPTY';
}
else
{
        while($row = $fetch->fetch( PDO::FETCH_ASSOC )) {
            if ($check > 0 && $row['status'] == 0) {
                echo "status  0";
                // or echo "<br/> BLANK!";
            }
            else
            {
                echo '<div class="background">'.$row['email'].' <span style="margin-left: 10px;"><font color="#ff0000">UNANSWERED</font></span><span id="right"><a href="index.php?recover='.$row['recover_id'].'">Read More</a></span></div><br />';
            }           
       }
}

5 Comments

It fixed the problem, but there's another problem: If I have rows, and all of them has the column 'status' with higher value of '0', it will echo nothing
I see, you should put the an else after the if inside the while loop. Try edited version.
So if you also want to show values with status >0 you can modify the if in above code and change the == comparison to >=
This is a solution, but in my opinion using WHERE stauts = 0 is way stabler and easier. Thanks for your answer too ^^.
Yeah.. that's a lot easier to hit it at MySQL..:)

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.