I'm running a MariaDB server (10.3.27-MariaDB-0+deb10u1 Raspbian 10) for a hobby PHP website (PHP 7.3.27-1~deb10u1 (fpm-fcgi)).
Yesterday I decided it was time to start making daily backups of the database and added script in a cron job for the purpose. After the cron job ran this night the website gave me this error:
Fatal error: Uncaught mysqli_sql_exception: Prepared statement needs to be re-prepared in [a].php:180
Stack trace:
#0 [a].php(180): mysqli_stmt->execute()
#1 [b].php(64): getTargetID(Object(mysqli), 'ha', 2020)
#2 [c].php(3): require('....')
#3 {main} thrown in funcs.php on line 180
The function getTargetID() is basically executing a query that return an ID from a view. Running the exact same query (against the same database) in TablePlus works fine.
If I restart the MariaDB server, the PHP code executes as expected again, until the backup script is ran again.
The backup script is basically this:
mysqldump -u ${USER} -h${HOST} -p${PASSWORD} --all-databases |gzip > ${SQLFILE}$
Edit: The mysqldump command is placed in a shell script (db_backup.sh) and is executed at midnight with a cron job.
The script is executed on a backup machine, which has a database user with the following privileges:
- Select
- Trigger
- Show view
- Lock tables
EDIT2:
The function that causes the error contains the following code:
function getTargetID(&$mysqli, $league, $year) {
$sql_query = <<<SQL
SELECT team_id
FROM standings
WHERE league = ? and season = ?
ORDER BY points DESC, goals_for DESC, team_name
LIMIT 1;
SQL;
$stmt = $mysqli->prepare($sql_query);
$stmt->bind_param("si", $league, $year);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$rows = $result->fetch_all(MYSQLI_ASSOC);
$team_id = $rows[0]["team_id"];
$result->free();
return $team_id;
}
else { echo "<!-- Error 107 -->"; }
$stmt->close();
}
Standings is a view that "outputs" a table for standings in a football league.
I'm a bit at a loss how to fix this, and I've so far been unable to find any solution through Google. Should something be added to the dump command?