I am running a while/foreach on a mySQL database and an array to check if it exists or not. It's supposed to loop 170 times but it loops over 12000 times. Why is that?
$my_rows = array();
while($row = mysql_fetch_assoc($run_query)){
$my_rows[] = $row;
foreach($my_rows as $row){
if(in_array_r($row['name'], $products)){
echo "Exists";
} else {
echo "Does not exist";
}
}
}
$my_rows = array();
while($row = mysql_fetch_assoc($run_query)){
$my_rows[] = $row;
if(in_array_r($row['name'], $products)){
echo "Exists";
} else {
echo "Does not exist";
}
}
What should be happening here is that you assign the result of mysql_fetch_assoc
to $my_rows
and then loop over the contents of $my_rows
. One loop. One of those loops doesn't need to exist.
$my_rows = mysql_fetch_assoc(...);
foreach($my_rows as $row){ ...do stuff here... }