gpt4 book ai didi

php - 从 SQL 中选择到数组中

转载 作者:行者123 更新时间:2023-11-30 23:44:34 24 4
gpt4 key购买 nike

我试图找到一些相关的答案,但没有找到任何有用的东西。

我想从一个表中选择 *(当前该表中有 5 行)并将结果存储在一个数组中。

这是我的职责。

public function listAllRides() {

$index = 0;
$row = array();
$data = array();
$result = array();

$resu = $this->conn->prepare("SELECT * FROM rides ");

$resu->execute();
$ride = $resu->get_result()->fetch_assoc();

// this echo is to check if the query works. But only the first row
// is printed here
echo json_encode($ride)."<br>";


if ($resu->execute()) {

while ($row = $resu->fetch()) {
$data[$index]['id'] = $row;
$data[$index]['unique_id'] = $row;
$data[$index]['destinatie'] = $row;

// also tried this :
//$data[$index]['id'] = $row['id'];
//$data[$index]['unique_id'] = $row['unique_id'];
//$data[$index]['destinatie'] = $row['destinatie'];

$index++;

echo json_encode($data)."<br>";

}
echo json_encode($data)."<br>";
$resu->close();

}
else {
return false;
}



}

我得到的结果:

{"id":34,"unique_id":"59f8afdc2bf201.64404165","destinatie":"dera","driver":"123","kmride":"112233","created_at":"2017-10-31 19:16:12","updated_at":null}

[{"id":true,"unique_id":true,"destinatie":true}]

[{"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true}]

[{"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true},{"id":true,"unique_id":true,"destinatie":true}]

[{"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true}]

[{"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true},"id":true,"unique_id":true,"destinatie":true},{"id":true,"unique_id":true,"destinatie":true}]

修改后的代码:

public function listAllRides() {

$index = 0;
$row = array();
$data = array();
$result = array();

$resu = $this->conn->query("SELECT * FROM rides ");

$ride = $resu->fetch_all(MYSQLI_ASSOC);
return $ride;
}

It works, like this .. thanks for the answer Ramsés Fernández

What if I need to narrow the select :

public function getRideByDestination($destinatie) {

$stmt = $this->conn->prepare("SELECT * FROM rides WHERE destinatie = ?");
$stmt->bind_param("s", $destinatie);

if ($stmt->execute()) {
$ride = $stmt->get_result()->fetch_assoc();
$stmt->close();

return $ride;
}
else {
return false;
}
}

它只返回一行。查询的第一行。

最佳答案

您不需要 prepare 语句,您可以使用此代码获取所有结果集。

   $resu = $this->conn->query("SELECT * FROM rides ");
$ride = $resu->fetch_all(MYSQLI_ASSOC);

$ride now 是一个与所有骑行记录相关联的数组

您在 if else 中中断了函数的执行流程,这是没有必要的。

if ($stmt->execute()) {
$result = $stmt->get_result();
while ($row = $result->fetch_assoc())
{
$array[]=$row;
}
$stmt->close();
return array;
}
return false;

关于php - 从 SQL 中选择到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47420417/

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