从不同 class 获取 fetchall 结果

Get fetchall result from different class

让我解释一下我想做什么。

在一个 DBcommand.php 文件中,我有:

class DBcommand Extends PdoDB { 
     public function selectAll() {
       $stmt = $this->dbh->prepare("SELECT hwid,username,pcname FROM users");
       $stmt->execute();
       $columnn = array();
       $result_returned = $stmt->fetchAll(PDO::FETCH_CLASS);
       foreach($result_returned as $key=>$result) {
         $columnn['hwid'] = $result->hwid;
         $columnn['username'] = $result->username;
         $columnn['pcname'] = $result->pcname;
         //return $columnn;
         //$columnn= new us($result->hwid,$result->username,$result->pcname);
       }
       return $columnn;
     }
}

我正试图在另一个名为 View.php

的页面上获取我的结果
$cmd = new DBcommand();
//$get = $cmd->getInfo('1234');
$result=$cmd->selectAll();

echo '<tr ><td ><input type="checkbox" class="checkthis" /></td><td>' . $result['hwid'] . '</td><td style="cursor:pointer;" onclick="alert(\'ok\')">' . $result['username'] . '</td><td style="cursor:pointer;" onclick="alert(\'ok\')">' . $result['pcname']. '</td><td><p data-placement="top" data-toggle="tooltip" title="Edit"><button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit" ><span class="glyphicon glyphicon-download-alt" onclick="myAjax()"></span></button></p></td>
  <td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="glyphicon glyphicon-trash"></span></button></p></td>
</tr>';
                                        ?>

我遇到的问题是,我的 selectall 有效,但我的 view.php 只能从中获得 1 个信息,第一个或最后一个。

即使我尝试在 view.php 部分做一些 while,它也永远不会得到所有结果。

假设我的 selectAll return array('123','azerty',azerty');array('6547 ','qwertty',qwerty'); ,我来自 selectAll 的 print_r 将显示我想看到的内容,但我的结果来自 view.php, 只会显示这两个结果之一。

--

我什至尝试创建另一个使用 $this->_hwid 的 class,所以我稍后会使用这个 class,但没有成功,因为它的对象在字符串结果中。

--

卡住了,感谢您的帮助。

您的 foreach 始终只填充数组中的一项。

试试这个:

 foreach($result_returned as $key=>$result) {
   $columnn[$key]['hwid'] = $result->hwid;
   $columnn[$key]['username'] = $result->username;
   $columnn[$key]['pcname'] = $result->pcname;
 }
public function selectAll() {
    $stmt = $this->dbh->prepare("SELECT hwid,username,pcname FROM users");
    $stmt->execute();
    return $stmt->fetchAll();
}

这是您需要的问题函数的全部代码。

您的其他代码也需要对您正在使用的技术有最低限度的了解。至少你必须意识到你从 table 中以数组的形式获取多行,通常使用 foreach() 命令循环。