0

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!

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Joe
  • 457
  • 3
  • 8
  • 26

3 Answers3

1

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

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

Cylindric
  • 5,858
  • 5
  • 46
  • 68
0

You may want to let MySQL encode that $page variable:

mysql_query(sprintf(
    "UPDATE `pages` SET `page_cache`='%s' WHERE `page_id`=%s",
    mysql_real_escape_string( $page ),
    intval( $page_id )  // assuming it's an int
));
pp19dd
  • 3,625
  • 2
  • 16
  • 21
0

Check whether output buffering is on in your /etc/php.ini: http://www.php.net/manual/en/outcontrol.configuration.php

Also, do an:

<?php echo ob_get_contents(); ?>

If it prints FALSE, then output buffering may not be enabled: http://php.net/manual/en/function.ob-get-contents.php

Suman
  • 9,221
  • 5
  • 49
  • 62