While inside While

1

I was trying to list one result of a select within another while but it only runs the second while once. That's what I tried to do.

$lojas = query("Select cliente,endereco_cliente,cidade_cliente,uf_cliente,cep_cliente,cnpj_cliente from cliente LIMIT 4;");

while (($dados = fgetcsv($fp, 0, ";")) !== FALSE) {
    $quant_campos = count($dados);
    for ($i = 0; $i < $quant_campos; $i++) {
        echo "</br>".$dados[$i]; 
        while ($row = $lojas->fetch_assoc()) {
            echo "</br>BD:".$row['cnpj_cliente'];       
        }      
    }
    echo count($dados);
}
    
asked by anonymous 14.01.2016 / 20:33

1 answer

0

You should not use fetch_assoc() > "while ($row = $lojas->fetch_assoc()) .

To fetch all records, use mysql_fetch_array() :

while($row =  mysql_fetch_array($lojas)) { ... }

fetch_assoc() is most commonly used when query returns a single record, usually when you search directly for the primary key.

    
20.01.2016 / 14:10