【问题标题】:Undefined index warning on PDO::fetchAll indexPDO::fetchAll 索引上的未定义索引警告
【发布时间】:2014-05-30 00:28:18
【问题描述】:

由于“未定义索引”警告有很多不同的潜在原因,因此我很难找到导致此问题的原因。

Notice: Undefined offset: 0 in D:\xampp\htdocs\inc\php_main.php on line 71

$singleresult = $result[0]; 行是第 71 行。我很肯定 $result[0] 已设置,因为我已使用 print_risset 检查进行了验证。我错过了什么吗?我有点希望我只需要在这里进行一次健全性检查。 :)

function execute ($sql, $bindingsarray) {
    $pdostmt = $this->db->prepare($sql);
    $pdostmt->execute($bindingsarray);
    $result = $pdostmt->fetchAll(PDO::FETCH_ASSOC);
    print_r($result);
    if (isset($result[0]) && isset($result[1])); {
        $singleresult = $result[0];
        return $singleresult;
    }
    return $result;
}

【问题讨论】:

    标签: php mysql pdo indexing undefined


    【解决方案1】:

    您的变量未设置并且您没有注意到,因为您的 if 行是错误的:

    if (isset($result[0]) && isset($result[1])); {
                                               ^ your problem
        $singleresult = $result[0];
        return $singleresult;
    }
    

    等同于:

    if (isset($result[0]) && isset($result[1])) {
    
    }
    $singleresult = $result[0];
    return $singleresult;
    

    因为; 你在你的条件之后。只需将其删除,结果应该是您所期望的:

    if (isset($result[0]) && isset($result[1])) {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-22
      • 2013-05-22
      • 2011-06-28
      • 2018-01-06
      • 2019-12-09
      • 2013-07-19
      • 1970-01-01
      相关资源
      最近更新 更多