1

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;
}
fusion3k
  • 11,568
  • 4
  • 25
  • 47
Eptun
  • 13
  • 4
  • 1
    The error says your insert query has a syntax error. Now what's your question? – Charlotte Dunois Apr 17 '16 at 14:59
  • There's no guarantee that another running script won't insert a row with the same `chattime` between your `SELECT` and `INSERT` queries. You should investigate [transactions](http://php.net/manual/en/pdo.begintransaction.php) and [isolation levels](http://dev.mysql.com/doc/refman/5.7/en/set-transaction.html) if this is a concern. – Matt Raines Apr 17 '16 at 15:35

1 Answers1

0

you can only do one query at a time with PDO prepare. you're running two. you will have to do it in 2 separate statements.

I guess I was wrong. Instead of removing the answer, I'll leave this link here as a reference.

PDO Support for multiple queries

It's supported, but with a few caveats.

Community
  • 1
  • 1
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • 2
    According to [PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)](http://stackoverflow.com/questions/6346674/pdo-support-for-multiple-queries-pdo-mysql-pdo-mysqlnd), PDO *does* in fact support more than one query at a time. Indeed, the OP's code works fine for me. However the syntax error does imply that this is the problem here... – Matt Raines Apr 17 '16 at 15:29