I'm making a chat and I stumbled across an error. The error is:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO guildchat(guildID, playerID, message, `time`, chattime) VALUES(?, ?,' at line 1' in C:\xampp\htdocs\sf\sexyutility.php:14 Stack trace:#0 C:\xampp\htdocs\sf\sexyutility.php(14): PDO->prepare('SELECT @cht := ...')#1 C:\xampp\htdocs\req.php(2659): chatInsert('tewtewt', 53, 35)#2 {main} thrown in C:\xampp\htdocs\sf\sexyutility.php on line 14
The code is:
function chatInsert($message, $guild, $player){
$time = time();
$chattime = $GLOBALS['db']->prepare("SELECT @cht := Max(chattime) AS chattimer FROM guildchat WHERE guildID = :guild; INSERT INTO guildchat(guildID, playerID, message, `time`, chattime) VALUES(:guild, :player, :msg, :timers, @cht + 1)");
$chattime->bindParam(":guild", $guild);
$chattime->bindParam(":player", $player);
$chattime->bindParam(":msg", $message);
$chattime->bindParam(":timers", $time);
$chattime->execute();
return $chattime->fetch(PDO::FETCH_ASSOC)['chattimer'] + 1;
}
chattime
between yourSELECT
andINSERT
queries. You should investigate transactions and isolation levels if this is a concern. – Matt Raines