【问题标题】:Notice: Undefined index in array result of native query doctrine注意:原生查询原则的数组结果中未定义索引
【发布时间】:2017-02-05 18:48:46
【问题描述】:

我在 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 数组的一些值,但总是得到通知未定义索引。我正在查看文档,但没有看到解决方案。

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

提前致谢!!!!

【问题讨论】:

    标签: sql doctrine-orm symfony dbal


    【解决方案1】:

    因为 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

    【讨论】:

      猜你喜欢
      • 2017-12-13
      • 2018-07-02
      • 2012-07-15
      • 2015-06-02
      • 2015-10-09
      • 2019-05-23
      • 1970-01-01
      • 2014-02-06
      相关资源
      最近更新 更多