0ndre_ 0ndre_ - 2 months ago 6
MySQL Question

Run php action for every element

I have a php script which retrieves data from mysql db.
Everything works fine, but my problem is that this

$result = $dao->joinedEvents($userId);
returns an array of numbers and what I would like to do is to run this
$secondResult = $dao->joinedEventsInfo($receivedIds);
for every ID and this script I'm using right now returns data only for one ID.

This is part of my php script:

$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);


And this is joinedEvents script:

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;
}


This is joinedEventsInfo script:

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;

}


Edit: Tha reason I need this is that I have two tables. In the first one I have just IDs and in the second one I have info. So first I need to get the IDs and then I need to get data for every ID I have just received.

Thank you very much , I'm totally stuck.

Answer

Based on the updated code snippets and the discussion below, it is found that $result is indeed an array, and the solution is:

$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);