MySQL:Num Rows无效

I've stumbled upon a simple MySQL error, and it seems my attempts of fixing it are effortless. The problem, it's not counting.

Before I go further, I know that mysql_* is Deprecated, and that I shouldn't use it. I should use mysqli_* or PDO.

This is my Query, and yes, the echo is just for testing.

$ms_sql = mysql_query("SELECT * FROM mijnsp WHERE sp_username = '".$user['username']."'");
    while ($mijnspusers = mysql_fetch_assoc($ms_sql)) {

    $ms_count = mysql_num_rows($ms_sql);

    if($ms_count <= 0){
        echo "Result is empty";
    }else{
        echo $mijnspusers['new_username'];
    }

I've tried to change the IF, but with no effect;

if($ms_count <= "0"){

or, like this

if($ms_count <= '0'){

Thank you in advance, Pascal

Call

$ms_count = mysql_num_rows($ms_sql);

before the while() loop.

You should do the mysql_num_rows() before going into the loop. If the num is 0, then you'll never run the loop to begin with.

Your code should be:

$ms_sql = mysql_query("SELECT * FROM mijnsp WHERE sp_username = '".$user['username']."'");

$ms_count = mysql_num_rows($ms_sql);

    if($ms_count == 0){
        echo "Result is empty";
    }else{
        while ($mijnspusers = mysql_fetch_assoc($ms_sql)) {
           echo $mijnspusers['new_username'];
        }
    }`