Perplexed here…. This code:
$qry = sprintf("INSERT INTO news_sites_homepage_grab
VALUES ('', %d, '%s', NOW(), NOW())",
$site_id, mysql_real_escape_string($html));
…is executed in a loop where $html changes every time. This code executes once but the next time, the script simply dies. No warnings/errors, nothing. $html is a string representing a webpage so it could be very long. I have upped the memory limit in PHP to 32M and set max_allowed_packet in MySQL to 16M but nothing.
Anyone have any ideas? Thanks!
UPDATE: Here is the function that is called within a loop.
function save_html($site_id, $html) {
global $db;
try {
$qry = sprintf("INSERT INTO site_grab VALUES ('', %d, '%s', NOW(), NOW())",
$site_id,
mysql_real_escape_string($html));
$db->insert($qry);
}
catch(Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
return;
}
My recommendation would be to forgo troubleshooting this issue and switch to PDO. It supports prepared statements, which aren’t vulnerable to SQL injection when you use parameters and are much more performant for repeated queries.
Here’s a simple refactoring of your function, which assumes
$dbholds a PDO instance:If
date_addedhas type TIMESTAMP, you can set its default value toCURRENT_TIMESTAMPand leave it out of the insert. Alternatively, add anON UPDATE CURRENT_TIMESTAMPproperty to thedate_modifiedcolumn, which means you don’t need to explicitly set the field when you update a row. If inserting is going to be more common than updating, do the former; otherwise, do the latter.You could also store prepared statements in an object so they’re accessible from multiple functions and replace the global
$dbwith some sort of service locator (the simplest way is to use a static function or property of some class) or use dependency injection (read “Inversion of Control Containers and the Dependency Injection pattern“).