Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何正确使用PHPSQlite3准备并绑定2个值?_Php_Sqlite - Fatal编程技术网

如何正确使用PHPSQlite3准备并绑定2个值?

如何正确使用PHPSQlite3准备并绑定2个值?,php,sqlite,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=':userna

尝试使用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()

您的问题是您将
$q
声明为
SQLite3Stmt
实例,但您正在绑定和执行
$stmt

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

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

你想做的事很危险。请任何回答你问题的人,如果没有任何警告,都不会关心你。我会检查他们,但我只需要知道我做错了什么,以及如何正确处理。请不要将密码存储为明文,永远不要。感谢你指出这一点..尽管我仍然收到对布尔函数上成员函数execute()的调用。。而且绑定是否应该以某种方式在一行中完成?就在
$stmt->execute()之后添加以下代码:
echo$db->lastErrorMsg()。。。这应该可以更好地解释错误消息。共享它显示的错误消息。@user837288我想我找到了您的问题。我编辑了我的答案以反映变化。不管我对bindValue问题的评论如何,您一开始就正确地绑定了它们。。几乎现在,类SQLite3Stmt的对象无法转换为if($stmt>0)语句的int刚刚编辑了最后一条注释感谢您的努力。。我现在正在尝试:如果($stmt>0){echo'成功登录。;}否则{echo'用户名/密码无效。;}