简体   繁体   中英

Save mysql query result into a PHP array

Here is the situation:

I have this array in PHP, which is intended to keep the data from the result of a query:

$returnData = array(
    'ID'                 => '',
    'NAME'               => '', 
    'DESCRIPTION'        => '',
    'STATUS'             => '',
    'STATUS_DESCRIPTION' => '',
    'LOCATION'           => '',
    '_ERROR'             => ''
);

I later execute a query where I would like to fill this array:

$fetchResourceQuery = sprintf('SELECT  RESOURCEID, RESOURCENAME, RESOURCEDESCRIPTION,
                                           T3.RESOURCELOCATIONNAME, T2.RESOURCESTATUSNAME,
                                           T2.RESOURCESTATUSDESCRIPTION
                                   FROM resource          T1
                                   JOIN resource_status   T2
                                   ON T1.RESOURCESTATUSID = T2.RESOURCESTATUSID
                                   JOIN resource_location T3
                                   ON T1.RESOURCELOCATIONID = T3.RESOURCELOCATIONID');

$resultSet = $DB->query($fetchResourceQuery);
        if($resultSet){
            while($row = $resultSet->fetch_assoc()){
                $returnData['ID']                   = $row['RESOURCEID'];
                $returnData['NAME']                 = $row['RESOURCENAME'];
                $returnData['DESCRIPTION']          = $row['RESOURCEDESCRIPTION'];
                $returnData['STATUS']               = $row['RESOURCESTATUSNAME'];
                $returnData['STATUS_DESCRIPTION']   = $row['RESOURCESTATUSDESCRIPTION'];
                $returnData['LOCATION']             = $row['RESOURCELOCATIONNAME'];
            }
            $json_data = json_encode($returnData, JSON_UNESCAPED_SLASHES);
            echo $json_data;
        }

So far this is good. The problem is, I think there is only one cycle inside the while worth of data being saved, then it overwrites inside the array for some reason, the output is this:

{"ID":"456","NAME":"Rack con Televisor #1","DESCRIPTION":"Televisor Sharp Aquos con Laptop, armado en 2011.","STATUS":"Active","STATUS_DESCRIPTION":"Can be reserved.","LOCATION":"First Floor, High School","_ERROR":""}

This is bad because there are 5 resources in the database, and only one of them is being stored.

How can I make the array store several values like a josn object? Since apparently the soultion I'm trying right now is not working.

$save = array();
while($row = $resultSet->fetch_assoc()){
    $returnData['ID']                   = $row['RESOURCEID'];
    $returnData['NAME']                 = $row['RESOURCENAME'];
    $returnData['DESCRIPTION']          = $row['RESOURCEDESCRIPTION'];
    $returnData['STATUS']               = $row['RESOURCESTATUSNAME'];
    $returnData['STATUS_DESCRIPTION']   = $row['RESOURCESTATUSDESCRIPTION'];
    $returnData['LOCATION']             = $row['RESOURCELOCATIONNAME'];
    array_push($save, $returnData);
}
$json_data = json_encode($save, JSON_UNESCAPED_SLASHES);
echo $json_data;

You are indeed overwriting the same object on each iteration. What you want is something like this:

$returnData = array(); // Make it an array

// ...
// ...

while($row = $resultSet->fetch_assoc()){
    $item = array();
    $item['ID']                   = $row['RESOURCEID'];
    $item['NAME']                 = $row['RESOURCENAME'];
    $item['DESCRIPTION']          = $row['RESOURCEDESCRIPTION'];
    $item['STATUS']               = $row['RESOURCESTATUSNAME'];
    $item['STATUS_DESCRIPTION']   = $row['RESOURCESTATUSDESCRIPTION'];
    $item['LOCATION']             = $row['RESOURCELOCATIONNAME'];

    $returnData[] = $item; // Add new item to array
}

Use this:

$returnData = array(); // Simply do this. Don't need all index fields
$index = 0;
while($row = $resultSet->fetch_assoc()){
    $returnData[$index] = array("ID"=>$row['RESOURCEID'],
    "NAME"=>$row['RESOURCENAME'],"DESCRIPTION"=>$row['RESOURCEDESCRIPTION'],
    "STATUS"=>$row['RESOURCESTATUSNAME'],"STATUS_DESCRIPTION"=>$row['RESOURCESTATUSDESCRIPTION'],
    "LOCATION"=>$row['RESOURCELOCATIONNAME']);
    $index++;
}
echo json_encode($returnData, JSON_UNESCAPED_SLASHES);
$newArray = array();
while($row = $resultSet->fetch_assoc()){
            $returnData['ID']                   = $row['RESOURCEID'];
            $returnData['NAME']                 = $row['RESOURCENAME'];
            $returnData['DESCRIPTION']          = $row['RESOURCEDESCRIPTION'];
            $returnData['STATUS']               = $row['RESOURCESTATUSNAME'];
            $returnData['STATUS_DESCRIPTION']   = $row['RESOURCESTATUSDESCRIPTION'];
            $returnData['LOCATION']             = $row['RESOURCELOCATIONNAME'];

          array_push($newArray, $returnData);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM