mysql_fetch_array () does not show all records

0
Hello, I'm having problems, in my database table I have 3 records, I use the code below to search for 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, $ nr has a value of 3, since there are 3 records, so far so good.

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

$ resMateriaProva = $Profile-> searchProfile ($ code);

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

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

Does anyone know what the error is?

Thanks in advance!

    
asked by anonymous 26.03.2015 / 22:23

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 do not need this line

$row    = mysql_fetch_row($res);

As you are not using the variable, it is very likely that it is removing the first result. Try to comment it and test, if it does not need to change mysql_fetch_array. This is because mysql_fetch_row takes one line at a time, and when you use it you already remove the first result and return the rest of the query. Since you do not use the $ row variable you do not realize you received the first result. I hope I have been clear.

    
26.03.2015 / 22:33