PHP 警告:遍历数组时出现非法字符串偏移量“”

PHP Warning: Illegal string offset '' while iterating through an array

我在尝试遍历数组时一直收到 "Warning: Illegal string offset " 错误。

我使用 var_dump 后的结果 ---> array(19) { ["id"]=> int(1) ["handle"]=> string(4) "test" ["blockname"]=> string(0) " " ["project"]=> 字符串(0) "" ["document_description"]=> 字符串(4) "test" ["project_id"]=> 字符串(0) "" [ "revision"]=> 字符串(0) "" ["scale"]=> 字符串(0) "" ["document_num"]=> 字符串(9) "test-1557" ["drawn_by"]=> 字符串(0) "" ["approved_by"]=> 字符串(0) "" ["checked_by"]=> 字符串(0) "" ["issued_by"]= > 字符串(0) “” ["drawn_date"]=> 字符串(0) “” ["approved_date"]=> 字符串(0) “” ["checked_date"]=> 字符串(0) "" ["issued_date"]=> 字符串(0) "" ["purpose"]=> 字符串(0) "" ["notes"]=> 字符串(0) "" }

所以我假设它实际上是一个数组?

然后我还在 运行 循环之前检查它是否是一个数组。错误重复了我期望循环到 运行 的次数,所以它似乎是 'iterating'。我只是没有得到预期的结果。

我的代码

$titles = $titleBlockLib->search($_POST['document_description']);

      ?>
      <h1>LFFN Data</h1>
        <input type="button" value="Add Row" onclick="titleBlock.addEdit()"/>
        <form onsubmit="titleBlock.search()">
        <label for="search">Search:</label>
          <input id = "search" type="text">
          <input id = "submit" type="submit" value="Search">
        </form>

    <?php
      var_dump($titles);
        if (is_array($titles)) {
            echo "<table class='zebra'>
            <tr><td>Handle</td><td>Description</td><td>Document Number</td><tr>";
            foreach ($titles as $t) {
                printf("<tr><td>%s</td><td>%s<td>%s</td><td class='right'>"
                  . "<input type='button' value='Delete' onclick='titleBlock.del(%u)'>"
                  . "<input type='button' value='Edit' onclick='titleBlock.addEdit(%u)'>"
                  . "</td></tr>", 
                  $t['handle'], $t['document_description'], $t['document_num'],
                  $t['id'], $t['id']
                );
              }
              echo "</table>";
            } else {
              echo "<div>No data found.</div>";
            }
            break;

保险起见-调用我的SQL语句的函数如下-->

function search($document_description){
    $sql = "SELECT * FROM `lffntitleblock` WHERE document_description LIKE '%".$document_description."%'";
    $this->stmt = $this->pdo->prepare($sql);
    $this->stmt->execute();
    $entry = $this->stmt->fetchAll();
    return count($entry)==0 ? false : $entry[0] ;

}

找到答案..

我的 sql 声明正在返回..

count($entry)==0 ? false : $entry[0] ;

这是一个布尔值。 删除此语句并返回 $this->stmt->fetchAll(); 后 问题已解决。