我如何正确使用php sqlite3准备并绑定2个值?

时间:2017-08-07 10:15:41

标签: php sqlite

尝试使用sqlite3创建登录页面

    $db = new SQLite3('mydb.db');
    $username = stripslashes(strip_tags($_POST['username']));
    $password = stripslashes(strip_tags($_POST['password']));

    $q    = $db->prepare("SELECT * FROM profile WHERE username=':username' and password=':password'");
    $stmt = $q ->bindValue(':username', $username, SQLITE3_TEXT);
    $stmt = $q ->bindValue(':password', $password, SQLITE3_TEXT);
    $stmt ->execute();
    $row  = $stmt->fetchArray();

    if($row > 0)
    {
        echo 'Successfully logged in.';
    }
    else 
    {
        echo 'Error';
    }

致命错误:未捕获错误:在布尔值上调用成员函数execute()

1 个答案:

答案 0 :(得分:1)

您的问题是,您将$q声明为SQLite3Stmt个实例,但您正在约束并执行$stmt

您必须使用$stmt初始化$db->prepare,以便在查询中正确绑定您的值,然后执行它。

$db = new SQLite3('mydb.db');
$username = stripslashes(strip_tags($_POST['username']));
$password = stripslashes(strip_tags($_POST['password']));

$stmt = $db->prepare("SELECT * FROM profile WHERE username = :username and password = :password");
$stmt->bindValue(':username', $username, SQLITE3_TEXT);
$stmt->bindValue(':password', $password, SQLITE3_TEXT);
$stmt->execute();

//Returns an array if user exists, if not then returns FALSE.
$exists  = $stmt->fetchArray();

if($exists === false)
{
    echo 'Error';
}
else 
{
    echo 'Successfully logged in.'; 
}