1

There is no further description of the error, the error above is the only thing that I'm getting. I also managed to get line of the error, and it is the one where I try to fetchAll results.

$id = (int) base64_decode($id);
try {
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->beginTransaction();
    $sql = "SET @i:=0; SELECT result.* FROM (SELECT @x:=@x+1 AS position, temp.* FROM (SELECT u.id, u.fbname, MAX( c.likeit + c.loveit + c.cute + c.aw ) AS total, c.likeit, c.loveit, c.cute, c.aw FROM currentphoto AS c LEFT JOIN users AS u ON u.id = c.id GROUP BY u.id ORDER BY total DESC) temp ) result WHERE id = :id";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':id', $id, PDO::PARAM_INT);
    $stmt->execute();
    $rows = $stmt->fetchAll(PDO::FETCH_OBJ);
    $conn->commit();
} catch (Exception $e) {
    $conn->rollBack();
    echo $e->getMessage();
}
if ($rows != null) {
    return json_encode(base64_encode($rows));
} else return null;

I just read somewhere that using mysql variables in PDO might require using declarations in separate queries. Is that right? And if so, how do I do it? Do I execute first statement with variable and the second statement after that?

If that isnt right, then what is the problem? Thanks guys

Vladimir Marton
  • 569
  • 4
  • 31

1 Answers1

1

The error you're facing is likely caused by using several statements in a single query.

You have two options here:

  • split them into a 1 statement/query and execute them subsequently (in your case, two queries)
  • use $conn->setAttribute(\PDO::ATTR_EMULATE_PREPARES, 1); to allow multiple statements in a single query

This answer explains ATTR_EMULATE_PREPARES well PDO MySQL: Use PDO::ATTR_EMULATE_PREPARES or not?

Community
  • 1
  • 1
Alex Tartan
  • 6,736
  • 10
  • 34
  • 45