0

This is my code for getting the mysql table content

I am getting whole data at once.

I want to put a time delay in each of the query.

$host= "localhost";
$user= "xxxxx";
$pass= "xxxx";
$db="xxxxx";

$connect= mysql_connect($host,$user,$pass);
if (!$connect)die ("Cannot connect!");
mysql_select_db($db, $connect);

$result = mysql_query("
    SELECT
        *
    FROM
        url
");

if($result){
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)){

$url = $row['urlss'];
echo '<li>'.$url.'</li>';
}
}

The result output is

row1
row2
row3
row4

I want to get output In below manner

row1
//wait 5 sec
row2
//wait 5 sec
row3
//wait 5 sec
row4
//wait 5 sec

Thanks

Vinay
  • 57
  • 1
  • 11
  • 6
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 26 '16 at 16:57
  • You can do that client-side with JavaScript. – Jay Blanchard May 26 '16 at 16:59
  • @JayBlanchard any example sir ? – Vinay May 26 '16 at 16:59
  • 1
    @Vinay That comment is literally full of links to examples. – tadman May 26 '16 at 17:02
  • 1
    Well there's so much already wrong with this code, why not just add a `sleep(5000)` in your while loop? – WillardSolutions May 26 '16 at 17:07
  • You need to use ob_flush(); flush(); sleep(5); output text, sleep 5 seconds – Nitin May 26 '16 at 19:22
  • on http(s) level browser opens socket to dedicated port and does GET /someurl, waits for output from serverside and then renders content. You cannot render html output while it's not outputted totally. So browser will wait 20 seconds and then immediately will show them all. – num8er May 26 '16 at 20:01

4 Answers4

3

Use client side script jquery/javascript to achive it.

PHP code:

<?php 
// code for DB connection and query

echo "<ul id='list-results'>";
if($result){
 while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $url = $row['urlss'];
    // display none 
    echo '<li style="display:none">'.$url.'</li>';
 }
}
echo "</ul>";
?>

Jquery:

// include jquery
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  // read all list tlements
 $( "#list-results li" ).each(function( index ) {
    // fade in each li
    $(this).delay(5000*index).fadeIn();
    });
});
</script>
Ravinder Reddy
  • 3,869
  • 1
  • 13
  • 22
  • No I want to get each row after 5 sec. In your code sir, I am getting all rows. they are just fading in after 5 sec. – Vinay May 26 '16 at 18:06
  • @Vinay, You're outputting html data, and it will not appear as You wish, cuz web browser keeps asking data from Your server and after it renders it. You can output them inside
  • some data
  • and then using jquery animate them. Ravinder Reddy is right with his answer! – num8er May 26 '16 at 19:54