Array bringing different results in PHP

1

I have my bd, however, it's bringing me a different result than the one in the bank

$consulta = "select * FROM PRODUTO_pedido WHERE PEDIDO_PED_ID =12";                        
$resultado = $db_con->query($consulta);
$contador = $resultado ->rowCount(); 
while ($row = $resultado-> fetch()){
  echo "<script>alert(".$row['PRODUTO_PROD_CODIGO'].")</script>";
}

For example, in bd the id is like 299007 and is displaying 01110105 .

How do I solve it?

    
asked by anonymous 18.07.2017 / 21:06

1 answer

0

Use the method fetch_array instead of fetch . Your code will look like this:

$consulta = "select * FROM PRODUTO_pedido WHERE PEDIDO_PED_ID=12";                        
$resultado = $db_con->query($consulta);
$contador = $resultado ->rowCount(); 
while ($row = $resultado-> fetch_array()){
  echo "<script>alert(".$row['PRODUTO_PROD_CODIGO'].")</script>";
}

Using fetch_array , you make explicit that you want to treat each line of the result of query as a array separately within the loop repetition.

Here is the documentation:

link

To use fetch, you need to determine an association (as seen in the documentation: php.net/manual/en/mysqli-stmt.fetch.php):

stmt_bind_assoc($db_con, $row);
    
18.07.2017 / 21:53