I want to replace the LIMIT quantity in a mySQL query to a variable.
This does work:
$x = $conectarDB->prepare("
  SELECT contenidoID, titulo, fecha, encabezado
  FROM contenidos
  WHERE publicado = 1
  ORDER BY fecha DESC
  LIMIT ".$limit."
  ");
$x->execute();
$y = $x->fetchAll(PDO::FETCH_ASSOC);
But this doesn't:
$x = $conectarDB->prepare("
  SELECT contenidoID, titulo, fecha, encabezado
  FROM contenidos
  WHERE publicado = 1
  ORDER BY fecha DESC
  LIMIT ?
  ");
$x->bindParam(1, $limit);
$x->execute();
$y = $x->fetchAll(PDO::FETCH_ASSOC);
I receive this error:
PHP 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 ''2'' at line 5' in /public_html/includes/footer-info-extra.php:25
How to change that number in a secure way? What am I doing wrong?
Thanks for your help!
Please note that I've been reading about how to do that in MySQL, I want to do it in php
