mysql_fetch_array() does not show all records

Asked

Viewed 2,115 times

0

Hello, I’m having problems, in my database table I have 3 records, I use the code below to fetch them:

public function buscarMateriaProva($codigoProva){
    $query  = " SELECT  codigoMateria, codigoProva, quantidadeQuestao
                FROM    materiasProvas
                WHERE   codigoProva = ".$codigoProva;

    //se for executado
    if($res = mysql_query($query)){
        $row    = mysql_fetch_row($res);
        $nr     = (int)mysql_num_rows($res);

        #se houver registro povoa o obj, senão retorna falso
        if($nr === 0){
            return false;
        } else {
            return $res;
        }

    } else {
        return false;
    }
}

In this case, the $nr has value 3, because it is 3 records, so far so good.

The value returns to my file where I mount the structure using mysql_fetch_array():

$resMateriaProva = $materiaProva->buscarMateriaProva($codigo);

while($arrayMateriaProva = mysql_fetch_array($resMateriaProva)){
    print_r($arrayMateriaProva);        
}

It returns the 3 records, normal, but only displays the last 2, ignoring the first record.

Someone knows what the mistake is?

From now on, thank you!

  • Mysql is susceptible to hacking because you don’t update to Pdo or mysqli?

1 answer

0


Because you do not replace mysql_fetch_array with mysql_fetch_row.

would look like this:

while($arrayMateriaProva = mysql_fetch_array($resMateriaProva)){
print_r($arrayMateriaProva);        
}

and you don’t need this line

$row    = mysql_fetch_row($res);

Apparently you are not using the variable, and it is very likely that it is the one that is removing the first result. Try to comment on it and test, if it works out you don’t even need to change the mysql_fetch_array. This is because mysql_fetch_row takes one line at a time, and when using it you already remove the first result and return the rest of the query. Since you don’t use the $Row variable you don’t realize that you received the first result. I hope I was clear.

  • Thanks, it worked, thanks for your help!

  • Glad I could help, it was a pleasure. But I bring another suggestion, from a glance in PDO, it makes it much easier in the management part with the database and already treats the parameters.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.