As you may have guessed from some of the questions I’ve been asking lately, my new job involves building some web pages using PHP and MySql. Even though I’ve been a programmer since about the time dirt was invented, I’m brand new at these tools.
Here’s the latest thing that’s got me stymied. I want to display the first part of the web page, then call a function that creates some data to be displayed.
To create the data, I’m reading from a .csv file with about 3500 lines. For each line I extract 5 data fields and insert them into a MySql record. Sounds easy, doesn’t it.
If I run through the code without doing the insert, and instead just display the data fields on the screen, using “echo” it runs great and finishes in no time at all.
When I put in the insert statement and load the page, this is what happens:
[ul]
[li]Nothing from the page displays on the screen. The previous page continues to display.[/li][li]Somewhere over 2,000 records are inserted into the table (number varies with each run). This takes several minutes.[/li][li]After records stop being inserted, the page displays up to the point where the function is called.[/li][/ul]
Has anyone else seen something like this before, and know of a way to prevent it?
You may want to try the mysql_real_escape_string() function ( PHP: mysql_real_escape_string - Manual ) on your input variables to escape any special characters which could be corrupting your query.
The query actually works, and the records are being inserted, until it quits.
The behavior is similar to the SDMB when it “times out” and you have to wait a while for the page to display.
I’m thinking that somehow the browser is timing out when it doesn’t get any response, then at that point MySql stops inserting records and the page displays.
FWIW, I’m running this on localhost, so the server and browser are both on the same machine.
Have you considered using Javascript to flash the records on the screen as they insert (or a number). Replace the value of a text field with the record number as it goes by.
We have an importing feature in one of our apps, and we do this. Except it’s ASP and MSSQL, not PHP and MySQL.
We also added a progress bar, and some text that says “HANG TIGHT!” (or something like that) just to keep people’s hands off of the page while it’s working. At the end, we redirect to another page.
Also, is there a PHP equivalent to Sever.ScriptTimeout? You might want to try adding that to your page.
I have nothing to add of any value, except that I think this is virtually impossible to troubleshoot without seeing your code. It’s basically all shot-in-the-dark type responses. The query looks fine, but wherever that query is in your PHP code is the important part.