2017-07-26 81 views
2

我試圖用準備好的聲明中對我的PHP SELECT查詢,但是我得到這個錯誤的Sql預處理語句的execute()錯誤

SQLite3Stmt ::執行()恰好預計0參數,1給出在C:\ xampp \ htdocs \ xport \ post.php on line 75

這裏有什麼問題?我可以參考我得到的答案here

但這不起作用。以下是我的代碼。

$postid = (int) $_GET['post_id']; 

$userpostquery = "SELECT * FROM users WHERE userid = ?"; 
$stmt = $db->prepare($userpostquery); 
$stmt->execute([$postid]); 

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
{ 
    $cname = $row['cname']; 

    echo $cname; 
} 

謝謝。

+1

'$ stmt-> execute([$ postid]);'remove'[$ postid]'。從那開始。 –

+1

似乎你使用的是http://php.net/manual/en/sqlite3stmt.execute.php,而不是http://php.net/manual/en/pdostatement.execute.php綁定請參見http:// php達網絡/手動/ EN/sqlite3stmt.bindvalue.php。 – chris85

+0

@ Fred-ii-如果我這樣做,現在我將如何獲得我在'bindpostquery'中找到的'bindValue'?與#postid'。 – diagold

回答

1

雖然SQLite語法類似於PDO,你不能在execute傳遞參數(請參見手冊 - http://php.net/manual/en/sqlite3stmt.execute.php,沒有參數此功能可用)。所以你需要使用bindParam/bindValue

二,execute()方法返回SQLiteResult,您應該遍歷它,現在通過$stmt

三,SQLiteResult已有沒有fetch方法,只有fetchArray。 和第四 - 因爲你不使用PDOPDO::常數是無用的。

$postid = (int) $_GET['post_id']; 

$userpostquery = "SELECT * FROM users WHERE userid = ?"; 
$stmt = $db->prepare($userpostquery); 
// treat $postid as INTEGER 
$stmt->bindParam(1, $postid, SQLITE3_INTEGER); 
// get SQLiteResult 
$result = $stmt->execute(); 

while ($row = $result->fetchArray(SQLITE3_ASSOC)) 
{ 
    $cname = $row['cname']; 

    echo $cname; 
}