How to fix the following error: Array to string Conversion?

Asked

Viewed 41 times

0

I’m trying to extrair os dados de quantidade de produtos from a table but I’m not getting.

I’ve tried using the Foreach but it didn’t work.

Also some way to remove data from table other than by format array?

Code below:

<?php

  require_once 'lib/conn.php';
  $sql = "SELECT SUM(quantidade) AS 'Quantidade de Produtos no Estoque'FROM tbl_estoque";
  $stmt = $conn->query($sql);
  $quantidade_estoque = $stmt->fetchAll(PDO::FETCH_OBJ);

  <td><?=$quantidade_estoque?></td>

?>
  • What is returning in this array? $quantidade_estoque could post the result of var_dump($quantidade_estoque);?

  • array (size=1) 0 => Object(stdClass)[3] public 'Quantity of Products in Stock' => string '33' (length=2) This is the output of var_dump

  • The mistake is clear: Array to string conversion You cannot print/view a array with an echo, as it is only used in a string; do the following: $quantidade_estoque[0] to take position 0 the array;

1 answer

2


Since the query only returns one column, and considering that you are using PDO, you can do so:

$quantidade_estoque = $stmt->fetchColumn();
  • 1

    Thank you very much, it worked right.

Browser other questions tagged

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