0

Here is my query:

SELECT qa.id,
       qa.subject,
       qa.category cat,
       qa.keywords tags,
       qa.body_html,
       qa.amount,
       qa.author_id author,
       qa.visibility,
       qa.date_time,
       COALESCE(u.reputation, 'N') reputation,
       COALESCE(CONCAT(u.user_fname, ' ', u.user_lname), 'ناشناس') name,
       COALESCE(u.avatar, 'anonymous.png') avatar,

  (SELECT COALESCE(sum(vv.value),0)
   FROM votes vv
   WHERE qa.id = vv.post_id
     AND 15 = vv.table_code) AS total_votes,

  (SELECT COALESCE(sum(vt.total_viewed),0)
   FROM viewed_total vt
   WHERE qa.id = vt.post_id
     AND 15 = vt.table_code
   LIMIT 1) AS total_viewed
FROM qanda qa
INNER JOIN qanda_tags qt ON qt.qanda_id = qa.id
INNER JOIN tags ON tags.id = qt.tag_id
LEFT JOIN users u ON qa.author_id = u.id
AND qa.visibility = 1
WHERE qa.type = 0
  AND tags.name = :t
ORDER BY total_votes DESC,
         qa.id DESC
LIMIT :j,
      11;

And this is print_r() the result of parameters' array: (which I pass it to the query as execute() argument)

Array ( [:t] => برچسب1 [:j] => 0 )

And when I execute the query, it throws this error:

Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\xampp\htdocs\myweb\others\questions.php:140 Stack trace: #0 C:\xampp\htdocs\myweb\others\questions.php(140): PDOStatement->execute(Array) #1 C:\xampp\htdocs\myweb\application\other.php(21): questions->index() #2 C:\xampp\htdocs\myweb\index.php(149): require_once('C:\xampp\htdocs...') #3 {main} thrown in C:\xampp\htdocs\myweb\others\questions.php on line 140

Can anybody find the problem? It's 1 hour I'm staring at it, and it seems fine to me.

Martin AJ
  • 6,261
  • 8
  • 53
  • 111

2 Answers2

0

You should use

 array( ':t' => برچسب1 ':j' => 0 )
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

You might be running into problems with the way PDO emulates parameters.

See my answer here: Parametrized PDO query and `LIMIT` clause - not working

So you should either turn off emulated parameters:

$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

Or else you MUST bind a PHP integer, and specify it as an integer:

$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
...
$stmt->bindValue('j', (int) $int, PDO::PARAM_INT);
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828