我正在尝试为我的 PHP SELECT
查询使用准备好的语句,但出现此错误
SQLite3Stmt::execute() expects exactly 0 parameters, 1 given in 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;
}
谢谢。
最佳答案
尽管 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;
}
关于php - SQL准备语句execute()错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45327318/