PHP警告:遍历数组时,字符串偏移量为非法

问题描述 投票:1回答:1

当尝试遍历数组时,我一直收到“警告:非法字符串偏移”错误。

使用var_dump之后的结果--->array(19){[“ id”] => int(1)[“ handle”] => string(4)“ test” [“ blockname”] => string(0)“” [“ project”] => string(0)“” [“ document_description”] => string(4)“ test” [“ project_id”] => string(0)“” [“版本”] => string(0)“” [“ scale” ] =>字符串(0)“” [“ [” document_num“] =>字符串(9)” test-1557“ [” drawn_by“] =>字符串(0)”“ [[” approved_by“] =>字符串(0)” “ [[” checked_by“] =>字符串(0)”“ [” issued_by“] =>字符串(0)”“ [” drawed_date“] =>字符串(0)”“ [”批准日期“] =>字符串(0 )“” [“ checked_date”] =>字符串(0)“” [“ issued_date”] =>字符串(0)“” [“用途”] =>字符串(0)“” [“笔记”] =>字符串(0)“”}

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

然后我还要在运行循环之前检查其数组。该错误重复了我希望循环运行的次数,因此似乎在“迭代”。我只是没有得到预期的结果。

我的代码

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

}
php arrays offset
1个回答
0
投票

找到答案..

我的sql语句正在返回..

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

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

© www.soinside.com 2019 - 2024. All rights reserved.