gpt4 book ai didi

php - 从数据库中检索具有特定 ID 的所有帖子

转载 作者:行者123 更新时间:2023-11-29 08:44:06 25 4
gpt4 key购买 nike

我想从数据库中检索具有特定 listId 的所有帖子,但只检索最后一个。下面是代码,我做错了什么?

来自 listModel.php:

public function GetListElements($listId) {

$query = "SELECT le.listElemId, le.listElemName, le.listElemOrderPlace, led.listElemDesc
FROM listElement AS le
INNER JOIN listElemDesc as led
ON le.listElemId = led.listElemId
WHERE le.listId=?";

$stmt = $this->m_db->Prepare($query);

$stmt->bind_param("i", $listId);

$listElements = $this->m_db->GetListElements($stmt);

return $listElements;
}

来自database.php:

public function GetListElements(\mysqli_stmt $stmt) {

if ($stmt === FALSE) {
throw new \Exception($this->mysqli->error);
}

//execute the statement
if ($stmt->execute() == FALSE) {
throw new \Exception($this->mysqli->error);
}

//Bind the $ret parameter so when we call fetch it gets its value
if ($stmt->bind_result($listElemId, $listElemName, $listElemOrderPlace, $listElemDesc) == FALSE) {
throw new \Exception($this->mysqli->error);
}

// Hämtar ids och användarnamn och lägger i arrayen.
while ($stmt->fetch()) {
$listElements = array('listElemId' => $listElemId,
'listElemName' => $listElemName,
'listElemOrderPlace' => $listElemOrderPlace,
'listElemDesc' => $listElemDesc);
}

$stmt->Close();

return $listElements; // contains only the last post in the table
}

表格

listElement:listElemId、listElemName、listId、listElemDescId、listElemOrderPlace

listElemDesc:listElemDescId、listElemId、listElemDesc

最佳答案

在每次迭代中覆盖 $listElements 变量,要修复它,您可以使用数组变量:

$listElements = array();
while ($stmt->fetch()) {
$listElements[] = array('listElemId' => $listElemId, //notice brackets: []
'listElemName' => $listElemName,
'listElemOrderPlace' => $listElemOrderPlace,
'listElemDesc' => $listElemDesc);

关于php - 从数据库中检索具有特定 ID 的所有帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12987030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com