2

I am trying to use prepared statement for my PHP SELECT query but i get this error

SQLite3Stmt::execute() expects exactly 0 parameters, 1 given in C:\xampp\htdocs\xport\post.php on line 75

What could be wrong here? I ma referencing the answer i got here

But this doesn't work.Below is my code.

$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;
}

Thanks.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
diagold
  • 493
  • 1
  • 7
  • 28

1 Answers1

1

Though SQLite syntax is similar to PDO, you can't pass parameters in execute (see the manual - http://php.net/manual/en/sqlite3stmt.execute.php, no arguments to this function are available). So you need to use bindParam/bindValue.

Second, execute() method returns SQLiteResult, you should iterate over it, now over $stmt.

Third, SQLiteResult has no fetch method, only fetchArray. And fourth - as you don't use PDO, PDO:: constants are useless.

$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;
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64