Fetch multiple rows from SQL in PHP foreach item in array
Posted
by
TrySpace
on Stack Overflow
See other posts from Stack Overflow
or by TrySpace
Published on 2012-11-12T10:57:14Z
Indexed on
2012/11/12
10:59 UTC
Read the original article
Hit count: 150
I try to request an array of IDs, to return each row with that ID, and push each into an Array $finalArray
But only the first result from the Query will output, and at the second foreach
, it skips the while
loop.
I have this working in another script, so I don't understand where it's going wrong.
The $arrayItems
is an array containing: "home, info"
$finalArray = array();
foreach ($arrayItems as $UID_get)
{
$Query = "SELECT *
FROM items
WHERE (uid = '" . cleanQuery($UID_get) . "' )
ORDER BY uid";
if($Result = $mysqli->query($Query))
{
print_r($UID_get);
echo "<BR><-><BR>";
while ($Row = $Result->fetch_assoc())
{
array_push($finalArray , $Row);
print_r($finalArray );
echo "<BR><><BR>";
}
}
else
{
echo '{ "returned" : "FAIL" }'; //. mysqli_connect_errno() . ' ' . mysqli_connect_error() . "<BR>";
}
}
(the cleanQuery is to escape and stripslashes)
What I'm trying to get is an array of multiple rows (after i json_encoded it, like:
{"finalArray
" : { "home
": {"id":"1","created":"0000-00-00 00:00:00","css":"{ \"background-color\" : \"red\" }"} }, { "info
": {"id":"2","created":"0000-00-00 00:00:00","css":"{ \"background-color\" : \"blue\" }"} } }
But that's after I get both, or more results from the db.
the print_r($UID_get);
does print info
, but then nothing..
So, why am I not getting the second row from info
? I am essentially re-querying foreach $arrayItem right?
© Stack Overflow or respective owner