This would helps me a lot to understand how does loop works
Let say i’ve database table my_table (id,words) and here is example of database
INSERT INTO `my_table` VALUES (1,'hello manal');
INSERT INTO `my_table` VALUES (2,'nice manal');
INSERT INTO `my_table` VALUES (3,'pretty manal');
now imagine for 100,000 entries (huge) and i want to replce the word manal to jack and giving me results every line changed one by one
i’ll use
$conn = mysql_connect('localhost','USER','PASS') or die(mysql_error());
mysql_select_db('my_table',$conn);
$sql = "SELECT * from my_table";
$result = mysql_query($sql,$conn);
while ($row = mysql_fetch_array($result)){
$old = $row['words'];
$id = $row['id'];
$new="jack";
$new = str_replace("manal", "$new", $old);
echo $new;
}
The loop
by that code it will works all at once which is impossible to my hosting server to apply all so i want to make it as loop ! i mean it will change the 1st database line then gives me the results echo $new; then change the 2nd database line then gives the results echo $new; and so on with no stop till last line.
so my important part is getting the results one by one ~thanks
The problem will be that your output is being buffered which is why you see all the results at the same time (at the end of the execution of the script).
You could add
ob_flush(); flush();within the loop beforeecho $new;an see if that helps. Regardless, the above example is going to execute pretty quickly so it isn’t exactly going to be easily readable!