Possible Duplicate:
How to go through mysql result twice?
I have a PHP script with a loop within a loop. The outer loop walks down an array for each $unit in the $unitsarray and query's a MySQL db to see if there is an entry with the same matching string. If $result1 does indeed return an entry(s), I create another array called $devicetokens array for the row "devicetoken" and then enter my second loop. For each device token, I create an Apple Push Notification to send to an iOS device. I have two problems, first the mysql_query returns nothing. If I replace $unit with a value I know will return na entry, then it works. Second, if I have replace the $unit and get a result back, the $devicetokens array wont populate with any data even though I have a result back from the mysql query. Here is my code:
foreach ($unitsarray as $unit) {
echo "Unit = $unit </br>";
// Create array of devices that match the unit
$result1 = mysql_query("SELECT * FROM `department devices` WHERE unit LIKE '%$unit%'") or die(mysql_error());
//Print results
while ($row = mysql_fetch_assoc($result1)) {
echo " ";
echo $row["device_id"];
echo " , ";
echo $row["devicetoken"];
echo " , ";
echo $row["unit"];
}
echo "</br>";
$devicetokenarray = array();
while ($row = mysql_fetch_assoc($result1)) {
array_push($devicetokenarray, $row["devicetoken"]);
}
// Print array
print_r($devicetokenarray);
echo "</br>";
// Loop APNS for each device token in $devicetoken array
foreach ($devicetokenarray as $devicetoken)
{
// Build the binary notification
$msg = chr(0).pack('n', 32).pack('H*', $devicetoken).pack('n', strlen($payload)).$payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
// Create APNS operation output
if (!$result)
echo 'Failed message'.PHP_EOL;
else
echo "<b>Successful message sent:</b> $call - $location - $station - $units to device(s): '$devicetoken </br>".PHP_EOL;
}
}
Here's what my db looks like:
device_id devicetoken unit
T05 ipad 773f5436825a7115417d3d1e036da20e806efeef547b7c3fe4 121
E05 ipad 773f5436825a7115417d3d1e036da20e806efeef547b7c3fe4 121
Any help would be greatly appreciated!