【问题标题】:Sql prepared statement execute() errorSql准备好的语句execute()错误
【发布时间】:2017-07-26 12:41:50
【问题描述】:

我正在尝试为我的 PHP SELECT 查询使用准备好的语句,但我收到此错误

SQLite3Stmt::execute() 需要 0 个参数,1 个在第 75 行的 C:\xampp\htdocs\xport\pos​​t.php 中给出

这里可能有什么问题?我会参考我得到的答案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;
}

谢谢。

【问题讨论】:

标签: php mysql sqlite prepared-statement


【解决方案1】:

虽然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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 2019-11-04
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多