开发者

How to properly backup results to the database?

开发者 https://www.devze.com 2023-03-22 06:30 出处:网络
I\'m working on 开发者_Go百科a jQuery AJAX call where I get a set of results from a search.I return html with an echo and I need to backup the results by inserting into the db.Will also have to check

I'm working on 开发者_Go百科a jQuery AJAX call where I get a set of results from a search. I return html with an echo and I need to backup the results by inserting into the db. Will also have to check if they already exist before inserting.

I want the html to be returned as soon as possible. If I add insert code below the echo will the entire code have to finish running before the html is returned?

The most important thing is content returning back to the user right away.

This is all on mobile so every 100 milliseconds count.

$data = file_get_contents($url);
$result = json_decode($data, true);

foreach ( $result->results as $items ) {

 $name = $items->name;
 $description = $items->desc;
 $id = $items->id;
 $coverurl = $items->coverurl;

 $returnhtml .= "<h3>".$name."</h3>";
 $returnhtml .= "<h4>".$description."</h$>";

}

echo $returnhtml;


//how to backup to database

//check if already in db
//insert into db


To answer your question, yes. The entire script must complete before the HTML is outputted back to the client. You can use output buffering to capture the output, send it, and then continue on with other processing. The server will then try to output the remaining when the script finished but AJAX doesn't know how to handle the second part so it just ignores it.

Look into http://www.php.net/manual/en/ref.outcontrol.php It'll be something like:

    ob_start();
    ... generate html for client
    ob_flush();// send output buffer to the client
    ... insert data into database
    ob_end_clean();


After do the ob_flush as explained before, you can try MySQL INSERT DELAYED if you don't wait for the INSERT to complete.

http://dev.mysql.com/doc/refman/5.5/en/insert-delayed.html

0

精彩评论

暂无评论...
验证码 换一张
取 消