gpt4 book ai didi

php - 为每个元素运行 php 操作

转载 作者:太空宇宙 更新时间:2023-11-03 11:44:36 26 4
gpt4 key购买 nike

我有一个从 mysql 数据库中检索数据的 php 脚本。一切正常,但我的问题是这个 $result = $dao->joinedEvents($userId); 返回一个数字数组,我想做的是运行这个 $ secondResult = $dao->joinedEventsInfo($receivedIds); 对于每个 ID,我现在使用的这个脚本只返回一个 ID 的数据。

这是我的 php 脚本的一部分:

$userId = htmlentities($_REQUEST["userId"]);

$result = $dao->joinedEvents($userId); //This is getting the IDs array

if(!empty($result)) {


$receivedIds = $result["event_id"];
$ids = explode(",", $receivedIds);

foreach($ids as $id){
$secondResult = $dao->joinedEventsInfo($id);
if(!empty($secondResult)) {
$returnValue["finalResult"][] = $secondResult;
} else {
$returnValue["status"] = "error";
$returnValue["message"][] = "Could not find records for id" . $id;
}
}


} else {
$returnValue["status"] = "Empty error";
$returnValue["message"] = "Could not find records";
}


$dao->closeConnection();
echo json_encode($returnValue);

这是 joinedEvents 脚本:

public function joinedEvents($userId){

$returnValue = array();

$sql = "SELECT event_id from MyTable WHERE userId= '$userId' LIMIT 0 , 30";
$statement = $this->conn->prepare($sql);
if (!$statement)
throw new Exception($statement->error);

$statement->execute();

$result = $statement->get_result();

while ($myrow = $result->fetch_assoc())
{
$returnValue[] = $myrow;
}

return $returnValue;
}

这是 joinedEventsInfo 脚本:

public function joinedEventsInfo($eventId){

$returnValue = array();

$sql = "SELECT * FROM Events WHERE eventId = '$eventId' LIMIT 0 , 30";

$statement = $this->conn->prepare($sql);
if (!$statement)
throw new Exception($statement->error);

$statement->execute();

$result = $statement->get_result();

while ($myrow = $result->fetch_assoc())
{
$returnValue[] = $myrow;
}

return $returnValue;

}

编辑:我需要这个的原因是我有两个表。在第一个中我只有 ID,在第二个中我有信息。所以首先我需要获取 ID,然后我需要获取我刚刚收到的每个 ID 的数据。

非常感谢,我完全被困住了。

最佳答案

根据更新后的代码片段和下面的讨论,发现$result确实是一个数组,解决方案是:

$userId = htmlentities($_REQUEST["userId"]);
$result = $dao->joinedEvents($userId);

if(count($result)){
foreach($result as $array){
$event_id = $array['event_id'];
$secondResult = $dao->joinedEventsInfo($event_id);
if(!empty($secondResult)) {
$returnValue["finalResult"][] = $secondResult;
} else {
$returnValue["status"] = "error";
$returnValue["message"][] = "Could not find records for id: " . $event_id;
}
}
}else {
$returnValue["status"] = "Empty error";
$returnValue["message"] = "Could not find records";
}
$dao->closeConnection();
echo json_encode($returnValue);

关于php - 为每个元素运行 php 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39560146/

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