2015-05-29 74 views
3

我的PHP是這樣的:綁定多個參數

$sql1="SELECT @rownum := @rownum + 1 Rank, q.* FROM (SELECT @rownum:=0) r,(SELECT * ,sum(`number of cases`) as tot, sum(`number of cases`) * 100/t.s AS `% of total` FROM `myTable` CROSS JOIN (SELECT SUM(`number of cases`) AS s FROM `myTable` where `type`=:criteria and `condition`=:diagnosis) t where `type`=:criteria and `condition`=:diagnosis group by `name` order by `% of total` desc) q""; 
$stmt = $dbh->prepare($sql1); 
$stmt->bindParam(':criteria', $search_crit, PDO::PARAM_STR); 
$stmt->bindParam(':diagnosis', $diagnosis, PDO::PARAM_STR); 
$stmt->execute(); 
$result1 = $stmt->fetchAll(PDO::FETCH_ASSOC); 
header('Content-type: application/json'); 
echo json_encode($result1); 

我在這條線得到一個錯誤:$stmt->execute();

錯誤說:

PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number' in php/rankings.php:39

堆棧跟蹤:

"#"0 php/rankings.php(39): PDOStatement->execute()

"#"1 {main} thrown in php/rankings.php on line 39

我該如何解決這個問題?我知道我可以通過一個準備好的聲明傳遞多個變量,但我不太確定如何去做。

+0

請告訴我們您的** **完全錯誤消息,其你得到並確保你向我們展示你的**真實**和正確的代碼。 – Rizier123

+0

你確定'$ search_crit'和'$ diagnosis'的內容嗎? – Random

+0

@Random是的...只是'var_dump'和值是正確的。 – jonmrich

回答

2

您可以在查詢中使用的參數只有一次

$sql1="SELECT @rownum := @rownum + 1 Rank, q.* FROM (SELECT @rownum:=0) r,(SELECT * ,sum(`number of cases`) as tot, sum(`number of cases`) * 100/t.s AS `% of total` FROM `myTable` CROSS JOIN (SELECT SUM(`number of cases`) AS s FROM `myTable` where `type`=:criteria and `condition`=:diagnosis) t where `type`=:criteria2 and `condition`=:diagnosis2 group by `name` order by `% of total` desc) q"; 
$stmt = $dbh->prepare($sql1);  
$stmt->execute(array(':criteria' => $search_crit, ':diagnosis' => $diagnosis, ':criteria2' => $search_crit, ':diagnosis2' => $diagnosis)); 
+0

這正是問題所在。我認爲這可能存在一些問題,但不知道該怎麼做。謝謝! – jonmrich

1

您可以添加一個數組來執行語句是這樣的:

$sql1="SELECT * FROM myTable WHERE `area` = :criteria AND `condition` = :diagnosis"; 
    $stmt = $dbh->prepare($sql1);  
    $stmt->execute(array('criteria' => $search_crit, 'diagnosis' => $diagnosis)); 
+2

OP的問題的答案在哪裏? – Rizier123

+2

這會引發相同的錯誤。 – jonmrich

+2

@jonmrich您確定您向我們顯示正確的文件是否正確嗎? – Rizier123