【发布时间】:2023-04-01 13:56:01
【问题描述】:
让我解释一下我要做什么。
在一个 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 部分做一些事情,它也永远不会得到所有结果。
假设我的 selectAll 返回 array('123','azerty',azerty'); 和 array('6547','qwertty',qwerty'); ,我的 print_r 来自 selectAll 将向我显示我想看到的内容,但我来自 view.php 的结果只会显示这两个结果之一。
--
我什至尝试创建另一个使用 $this->_hwid 的类,所以我稍后会使用这个类,但没有管理,因为它的对象是字符串结果。
--
卡住了,谢谢你的帮助。
【问题讨论】:
-
您正在覆盖
foreach循环中的值。你为什么要在DBcommand中注入PdoDB的对象,你已经在扩展它了。 -
是的,谢谢我更正了数据库问题,这是由于旧测试造成的。是的,在这个例子中,我重写了,但即使我将返回值放在 foreach 中,结果也会提示相同
标签: php mysql class pdo fetchall