Jump to content

Recommended Posts

Hi. I have code that does a SQL query which can take ten seconds or so. I figured rather than have people just sit there wondering if it's working, I'd display something like "Please wait ..." then after the query display the data. I did:

 

ob_end_flush();
echo "Please wait ...<BR><BR>";
(sql stuff)
echo "Done!";

 

...however it waits until the whole thing is done before it displays anything. I thought from ob_end_flush would do it. What would? Thanks!

Link to comment
https://forums.phpfreaks.com/topic/270211-cant-get-buffering-to-cooperate/
Share on other sites

ob_end_flush() would do that...if you put it AFTER the echo and not before.  But it won't work on all hosts, and it's not all that reliable.  Better to print the page and then perform the query in ajax so you can make a JS spinner with custom timeouts and stuff.

Edited by ManiacDan

Hmm, I'll have to read up on ajax. As for the SQL, it's not so much the time it takes for SQL to send data back as it is the time for me to process it. Orig, I was doing something like SELECT SUM(numberfield) AS blah but then someone put in a date range for a whole year of data and I got an arithmetic failure. Apparently it chokes trying to sum 35,000 records worth of numeric fields that can get large (it's seconds that a service ticket has been open, and some tickets go 60+ days). I switched to just doing SELECT numberfield then doing a while ($row = $sql->fetch(PDO::FETCH_ASSOC)) $tot += $row["numberfield"];. The looping is what takes so darned long.

 

Thanks guys.

A) Do you really need seconds resolution? You could divide the values, inside the sum() term, by 60 or 3600 to get minutes or hours resolution.

 

B) What's the goal of doing this anyway? Wouldn't you sum just the values for tickets that are still 'open'?

 

C) You can do a PDO fetchall for just that column and do a php array_sum to get the result. No loop needed.

Edited by PFMaBiSmAd

A - Good idea, maybe that will stop the SQL from crapping itself. I'll try that.

B - Average time a service ticket was open. System logs open time and last activity time (UNIX time) so what I'm actually doing is a sum of ticket end-ticket open where ticket status = closed.

C - Cool, wasn't aware of that feature. If A still fails, I'll try that.

 

Thanks a lot for the tips!

 

EDIT: Just tried it, A works like a champ. My SQL:

 

SELECT AVG(DATEDIFF(second, created, lastupdate)/60) AS ddiff FROM tickets WHERE ticket_status = 0 AND created >= CAST('2011-11-01' AS DATETIME) AND created <= CAST('2012-11-03' AS DATETIME)

 

Gives me back average minutes open on 35k records in a practically an instant. Whoo hoo :)

 

Thanks again!

Edited by Strahan
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.