0
$conn = getConn();
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "select * from posts where fk_user_id in (select id_user_1 from contacts where id_user_2=:_id) or (select id_user_2 from contacts where id_user_1=:_id) or :_id order by date desc limit 15 offset :_offset";
$stmt = $conn->prepare($sql);
$stmt->bindParam('_id', $id);
$o = "0";
$stmt->bindParam('_offset', $o);

Connection failed: 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 ''0'' at line 1

For some reason it doesn't bind the param correctly; if I manually put a 0 in the SQL everything works.

Fix: I fixed if by adding PDO::PARAM_INT. $stmt->bindParam(':_offset', $offset, PDO::PARAM_INT);

alps
  • 183
  • 1
  • 10

1 Answers1

0

You're binding parameters incorrectly. It should be:

$stmt->bindParam(':_id', $id);
$offset = 0;
$stmt->bindParam(':_offset', $offset, PDO::PARAM_INT);
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • That's not the issue – alps Feb 03 '16 at 08:15
  • @alps You're binding your parameters incorrectly. Apart from that what's the issue? – Rajdeep Paul Feb 03 '16 at 08:17
  • I've been binding exactly like this for months and I haven't had issue with it yet, I also tried with the columns and I had the same error.. – alps Feb 03 '16 at 08:18
  • Refer this, [http://php.net/manual/en/pdostatement.bindparam.php#refsect1-pdostatement.bindparam-examples](http://php.net/manual/en/pdostatement.bindparam.php#refsect1-pdostatement.bindparam-examples). Did you run the code with the `->bindParam()` I suggested? – Rajdeep Paul Feb 03 '16 at 08:23
  • I fixed if by adding PDO::PARAM_INT. $stmt->bindParam(':_offset', $offset, PDO::PARAM_INT); – alps Feb 03 '16 at 08:26
  • @alps Glad the issue is resolved. :) – Rajdeep Paul Feb 03 '16 at 08:28