Search code examples
phpmysqloutput-buffering

PHP: How can I cache a page using output buffer?


I'm trying to cache a page that gets/processes data from very slow API's - so I can load it quickly to the user. But for some reason the output buffer is empty?

<?php

ob_start();

// here I have php scripting accessing api's for information

?>


// Here I have HTML content with some php conditions and echos to filter and display the gathered information

// then I try to save the buffered page to the database:

<?php

//connect to database
$page = ob_get_contents();

mysql_query("UPDATE `pages` SET `page_cache` = '" . $page . "' WHERE `page_id` = '" . $page_id . "'");

?>

Any help would be appreciated!


Solution

  • Are you making sure your $page contains only database-safe characters?

    What happens if the output contains a single ' for example?