2

I get array result of a native query in Doctrine. The query is

    $conn = $em->getConnection();
    $sql = 'SELECT * FROM Stock_drink d WHERE id=?';
    $stmt = $conn->prepare($sql);
    $stmt->bindValue(1,$idstockdrink);
    $stmt->execute();
    $stockdrink = $stmt->fetchAll();

The $stockdrink content is

"{"stockdrink":[{"id":"39","name":"limon","stockamount":"14","price":"2.20","Drink_id":"5","Bar_id":"12"}]}"

And now i want to get some values of $stockdrink array with these sentences but always get the notice undefined index. I was looking de docs and i haven't seen the solution.

    $iddrink = $stockdrink["name"];
    $name = $stockdrink["name"];
    $price = $stockdrink["price"];

Thanks in advance!!!!

DBCooper
  • 73
  • 8

1 Answers1

3

Because fetchAll() method "fetches all results into an array", so in your particular case you have to modify your code this way:

$conn = $em->getConnection();
$sql = 'SELECT * FROM Stock_drink d WHERE id=?';
$stmt = $conn->prepare($sql);
$stmt->bindValue(1,$idstockdrink);
$stmt->execute();

$stockdrinks = $stmt->fetchAll();

$stockdrink = $stockdrinks[0];

$iddrink = $stockdrink["name"];
$name = $stockdrink["name"];
$price = $stockdrink["price"];

More on this here http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html#fetchall

Jan Rydrych
  • 2,188
  • 2
  • 13
  • 18