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!

9
  • What is the output if you var_dump($page) instead of issuing a query? Commented May 21, 2012 at 15:16
  • You should call ob_end_clean() at the end of working with output buffer. Try to comment ob_start() - do you see any output? Commented May 21, 2012 at 15:18
  • @watcher hi watcher var_dump($page) has no output :S Commented May 21, 2012 at 15:22
  • hi jan what do you mean by comment it? Commented May 21, 2012 at 15:23
  • What Jan means is what do you see if you don't buffer anything, but just display the page. Commented May 21, 2012 at 15:27

3 Answers 3

1

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

I try to echo or print_r $page before it gets to the database bit and it prints nothing
Hmm okay, so it doesn't seem to be a database query error. (Although you do need to sort that too)
cool thanks for the heads up for when I hopefully get this working :)
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
));

1 Comment

@Joe, this is basically what I was talking about to keep your inserts clean :)
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.