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

标签 php mysql arrays

我有一个从 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/

相关文章:

c++ - 将数组转换为 vector 的最简单方法是什么?

php - 将内容放入 iframe 中(在 ie 中中断)

php - 如何在SQL中打印表名包含对象的行?

php - 通过递归函数创建结构化数组

php - mySQL 数据库 : printing specific row or rows from a db table

mysql - 错误号 : 1452 Constraint

MySQL 数据库导入程序

php - Laravel 查询异常

jQuery:从数组中选择元素

c# - C# 中的结构编码(marshal)