0

i get this error, when i use select

$listchat = $pdo->prepare("SELECT * FROM listchat WHERE ?, userkey=?, ORDER BY idroom ASC LIMIT ?");

then

$num = 10;
$gid = 0;
$listchat->execute([$gid,$userKey,$num]);

I get the post title error, where am I wrong?

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' userkey='66756',

Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

0

You're missing the column to compare with $gid. And conditions are combined using AND or OR, not commas.

$listchat = $pdo->prepare("
    SELECT * 
    FROM listchat 
    WHERE gid = ? AND userkey=? 
    ORDER BY idroom ASC 
    LIMIT ?");

You also need to use bindParam or bindValue to supply a value for LIMIT, so you can specify that it should be treated as an integer, not a string.

$listchat->bindValue(1, $gid);
$listchat->bindValue(2, $userKey);
$listchat->bindValue(3, $num, PDO::PARAM_INT);
$listchat->execute();
Barmar
  • 741,623
  • 53
  • 500
  • 612