0

I an printing results of a MySQL query in a non-buffered way.

$mysqli  = new mysqli("localhost", "", "", "");
$uresult = $mysqli->query($sql, MYSQLI_USE_RESULT);  

if ($uresult) {
while ($row = $uresult->fetch_assoc()) {
//echo row   
}
}
$mysqli->close();

The user sees a stream of results, rather than having to wait for the final result.

However, when I activate this through jQuery post, like this, the non-buffering effect that I want fails. The user once again needs to wait for completion.

$("button").click(function(){
searchTerm: searchTerm
  $.post("test.php", function(data){
  $("#results").html(data)
  });
});

So I am back to square one! How can I get a stream of results?

Ned Hulton
  • 477
  • 3
  • 12
  • 27
  • No, according to this, it doesn't work like that: https://stackoverflow.com/questions/9008225/jquery-and-transfer-encoding-chunked "Will it wait until it receives the entire response before firing the success function? "Yes it will wait." – Ned Hulton Nov 14 '22 at 21:38
  • 1
    Read the answer of that post! You need jQuery stream, or better don't use jQuery at all. – Markus Zeller Nov 14 '22 at 21:39
  • https://stackoverflow.com/questions/9008225/jquery-and-transfer-encoding-chunked – Markus Zeller Nov 14 '22 at 21:41
  • You would need your PHP app to provide a websocket server (see https://stackoverflow.com/questions/14512182/how-to-create-websockets-server-in-php) and you would need your JavaScript client to consume that websocket stream (see https://javascript.info/websocket). IMHO, it's usually not worth it. You shouldn't be streaming such large result sets to the client that you would need this. – Bill Karwin Nov 14 '22 at 21:47
  • Why not even split the results and do multiple (paginated) requests? – Markus Zeller Nov 14 '22 at 21:49
  • I don't see what using non-buffered SQL queries has to do with streaming the data to the frontend. These are two completely different things. – Dharman Nov 14 '22 at 21:52
  • All I want is a simple counter. So rather than saying, "you have 5 million results", I am looking for some way to have it count from 0 to 5 million so that the user doesn't go insane with boredom. There is pagination, but I also need a total results counter. jQuery-stream looks promising, but how am I supposed to use it? If I have one PHP page echoing a number, what is the approach, something like this? $.stream("test.php", {$("#results").html(data)}); – Ned Hulton Nov 14 '22 at 21:53
  • jQuery-stream doesn't even work, "Uncaught TypeError: Cannot read properties of undefined (reading 'webkit')" and then "Uncaught SyntaxError: Unexpected string" – Ned Hulton Nov 14 '22 at 22:00

0 Answers0