Losing mysql result resource after first use
I have the following code: (query added)
$weight_table = mysql_query ("SELECT * FROM ".$prefix."weight ORDER BY wlimit DESC");
foreach ($weight as $area => $weight) {
echo $weight;
while ($weight_row = mysql_fetch_array($weight_table)) {
print_r($weight_row);
$limit = $weight_row['wli开发者_StackOverflow社区mit'];
if ($weight < $limit) {
$weight_level[$count] = $weight_row;
}
}
}
print_r($weight_level);
Where $weight_table is a mysql query result and $weight is an array.
My problem is that $weight_table seems to be expiring after the first while loop, so that the $weight array is effectively treated as having one item. This is a problem!
Can anyone suggest why php would forget a mysql result after the first use?
The first iteration through the for loop, you're fetching all of the records from the mysql result set. Your inner while loop will run until all the rows are gone. How many rows do you want it to pull?
Also you're re-using $weight which is a bad idea and may be causing side effects.
foreach ($weight as $area => $wt) {
echo $wt;
while ($weight_row = mysql_fetch_array($weight_table)) {
print_r($weight_row);
$limit = $weight_row['wlimit'];
if ($wt < $limit) {
$weight_level[$count] = $weight_row;
}
}
mysql_data_seek($weight_table,0);
}
print_r($weight_level);
You override $weight
array in the foreach declaration.
Use $area => $_weight
instead.
while ($weight_row = mysql_fetch_array($weight_table)) {
print_r($weight_row);
foreach ($weights as $area => $weight) {
echo $weight;
$limit = $weight_row['wlimit'];
if ($weight < $limit) {
$weight_level[$count] = $weight_row;
}
}
}
print_r($weight_level);
Use $weight array as $weights
foreach ($weight as $area => $weight) {
You're overwriting what was in your $height
array, which is why it is expiring after the first run through your loop. Try using another variable name for one or the other.
精彩评论