2017-07-07 87 views
0

這是我的查詢:無效的參數數:參數沒有在{路徑}定義

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; 

這是print_r()參數陣列的結果:(我將它傳遞給查詢作爲​​參數)

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

當我執行查詢,它拋出這個錯誤:

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

任何人都可以找到問題嗎?我盯着它1個小時,對我來說似乎很好。

+1

我們可以看到你綁定參數的代碼 –

回答

0

您應該使用

array(':t' => برچسب1 ':j' => 0) 
0

你可能會運行與PDO模擬參數的方式問題。

見我的答案在這裏:Parametrized PDO query and `LIMIT` clause - not working

所以,你應該可以關閉模​​擬參數:

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

否則你必須綁定一個PHP整數,並將其指定爲一個整數:

$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); 
... 
$stmt->bindValue('j', (int) $int, PDO::PARAM_INT);