So this is my first attempt at converting some previously used code to prepared statements in order to prevent SQL injections. I'm using mysqli procedural, as follows,
Old query:
mysqli_query($con,"UPDATE ulogs SET invalid_hits = invalid_hits + 1 WHERE user_id = $link AND date = '$date'");
if(mysqli_affected_rows($con) < 1) {
mysqli_query($con,"INSERT INTO ulogs(user_id,date,invalid_hits,unique_hits,non_unique_hits,earned,ref_earned,bonus) VALUES ('$link','$date',1,0,0,0,0,0)");
}
Prepared query:
$q1 = "UPDATE ulogs SET invalid_hits = invalid_hits + 1 WHERE user_id =? AND date =?";
$qa1 = "INSERT INTO ulogs (user_id,date,invalid_hits,unique_hits,non_unique_hits,earned,ref_earned,bonus) VALUES (?,?,1,0,0,0,0,0)";
if ($stmt = mysqli_prepare($con, $q1)) {
mysqli_stmt_bind_param($stmt,"is", $link, $date);
mysqli_stmt_execute($stmt);
$ucheck = mysqli_stmt_affected_rows($stmt);
mysqli_stmt_close($stmt);
}
if ($ucheck < 1) {
if ($stmt = mysqli_prepare($con, $qa1)) {
mysqli_stmt_bind_param($stmt,"is", $link, $date);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
}
mysqli_close($con);
exit();
And many more which get triggered under different circumstances but basically the same UPDATE first, then INSERT if nothing was updated. Apparently it worked as I based my queries on examples from php.net. Apparently... Because, after about 1 hour later, Apache was returning 500 server errors with php timing out while waiting for data from the db. Whereas with non-prepared queries such thing never happened.
So question is: have I done something wrong here? $stmt was being closed every time, along with $con so I don't know what may have caused the db to hang. Note that the server load did not went up either. It was just php-fpm reaching max clients due processes waiting for the db.
access_log
orerror_log
file of apache. – Progman