我正在尝试为我的 PHPSELECT
查询使用预准备语句,但我收到此错误
SQLite3Stmt::execute(( 期望正好 0 个参数,1 在第 75 行的 C:\xampp\htdocs\xport\post.php 中给出
这里可能出了什么问题?我参考了我在这里得到的答案
但这行不通。下面是我的代码。
$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;
}
谢谢。
虽然SQLite
语法类似于PDO
,但你不能在execute
中传递参数(请参阅手册 - http://php.net/manual/en/sqlite3stmt.execute.php,没有可用的参数(。所以你需要使用bindParam
/bindValue
.
其次,execute()
方法返回SQLiteResult
,您应该遍历它,现在遍历$stmt
。
第三,SQLiteResult没有fetch
方法,只有fetchArray
。 第四,由于您不使用PDO
,PDO::
常量是无用的。
$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;
}