注意:本机查询原则的数组结果中未定义的索引

时间:2017-02-05 18:48:46

标签: sql doctrine-orm symfony dbal

我在Doctrine中获得了本机查询的数组结果。查询是

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

$ stockdrink内容

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

现在我想用这些句子得到$ stockdrink数组的一些值,但总是得到通知未定义的索引。我正在寻找de docs,但我还没有看到解决方案。

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

提前致谢!!!!

1 个答案:

答案 0 :(得分:3)

因为fetchAll()方法"将所有结果提取到数组中,所以在您的特定情况下,您必须以这种方式修改代码:

$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"];

此处有更多相关内容http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html#fetchall