0

I need to pass a dynamic variable for OFFSET inside the query. I have searched and found out that since Mysql 5.6 this is supported. I am using version 5.6.25 but I still get a syntax error.

Here is my code.

try {
    $DBH = connect_sesiones();
    $SMT = $DBH->prepare("
        SELECT msg.id AS msgId,
               msg.id_emisor AS msgIdEmisor,
               msg.id_receptor AS msgIdReceptor,
               msg.mensaje AS msgMensaje,
               msg.leido AS msgLeido,
               msg.fecha AS msgFecha,
               us.nombre AS userNombre,
               us.apellido AS userApellido
          FROM (mensajes msg
         INNER JOIN usuarios us ON (CASE WHEN msg.id_emisor = :sessionUserId THEN us.id = msg.id_receptor WHEN msg.id_receptor = :sessionUserId THEN us.id = msg.id_emisor END))
         WHERE (msg.id_emisor = :sessionUserId AND msg.id_receptor = :contactId) 
            OR 
               (msg.id_emisor = :contactId AND msg.id_receptor = :sessionUserId) 
         LIMIT 8, OFFSET :offset
        ");


        $SMT->bindParam (':sessionUserId',checkSessionId());
        $SMT->bindParam (':contactId',$contactId);
        $SMT->bindParam (':offset',$offset);
        $SMT->execute();

And here is the error

Error: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 ''4'' at line 6

My question is, is version 5.6.25 supporting this feature?

Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75
netishix
  • 183
  • 4
  • 10
  • sorry, my mistake, edited! – netishix Feb 20 '16 at 00:13
  • Are you sure you get this error message for _exactly that_ query? a) the line number points to some rather uninteresting part of the query statement b) you should have gotten another error because `LIMIT x, OFFSET y` is invalid, it's _either_ a comma _or_ the keyword OFFSET, not both. – VolkerK Feb 20 '16 at 00:30

0 Answers0