is there anyway to return an array of strings that corresponds to the fetched row one by one?
Basically, if I want to retrieve all rows from my database, I do something like this:
$.post('my_script.php', {id:id}, function(data){
$('#container').html(data);
});
my_script.php
<?php
$id = $_POST['id'];
$query = "SELECT image_name FROM my_list WHERE id=$id";
if($mysql_query = mysql_query($query)){
while($mysql_fetch_assoc = mysql_fetch_assoc($mysql_query)){
$mysql_result_name = $mysql_fetch_assoc['image_name'];
echo "<img src='".$mysql_result_name."'/>";
}
}
?>
The problem about this is that, it will show a blank page for couple of second as long as this statement (while($mysql_fetch_assoc = mysql_fetch_assoc($mysql_query))) is not finished. This is definitely not good for the users. I want at least to display the image one by one if they are ready to be displayed.
It's basically impossible because of the fact that PHP is a server-side language and thus data is available when it has finished the script.
So you can't do such thing and have to wait until the script is complete
Or you can set the AJAX so that it will retrieve the rows one-by-one, That is the solution that comes to my mind right now.
EDIT:
As a better solution you can append a flush()
to the end of your while
so that it will print the image when it's ready but it's not a 100% solution. it MAY or MAY NOT work depending on your server and the method that you're calling the PHP page from. For example with AJAX, your success
function will not be called until it receives the 200 OK
response from the server which is sent when the page has fully loaded.
Edited code:
<?php
$id = $_POST['id'];
$query = "SELECT image_name FROM my_list WHERE id=id";
if($mysql_query = mysql_query($query)){
while($mysql_fetch_assoc = mysql_fetch_assoc($mysql_query)){
$mysql_result_name = $mysql_fetch_assoc['image_name'];
echo "<img src='".$mysql_result_name."'/>";
flush();
}
}
?>