PHP 和 mysql 的 select 出现奇怪的错误

标签 php mysql authentication

我是网页设计的初学者,我遇到了这个问题。我正在尝试创建登录页面,但是当我尝试创建登录时,它会抛出如下错误:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':username and passwordhash=:passwordhashed)' at line 1

带有 php 代码

Try { 
//    $SQL = 'INSERT INTO Passwords (username, password, passwordhashed) VALUES (:username,:password,:passwordhashed);';
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$PasswordHashed = sha1($password);
echo "Username: ". $username ."<br> Password: ". $password . "<br> PasswordHashed: " . $PasswordHashed;
$SQL = null;
$SQL = "SELECT * FROM BlaBla WHERE (username=:username and passwordhash=:passwordhashed);";
$Statement = $MySQL->prepare($SQL);
$Statement->bindValue(':username', $username);
$Statement->bindValue(':passwordhashed', $PasswordHashed);
$Statement->execute();
$Statement = $MySQL->query($SQL);
if ($Statement->rowCount() < 1 ) {
    echo 'NOPE';
} else {
    echo 'welcome back '. $username;
}

} catch(PDOException $e) {
$ErrorTitle = 'Error';
$Error = "error writing to database";
$ErrorInfo = '<p>Please contact administrator at stephan.littel@stecasso.nl</p> <br> <p>'. $e->getMessage() . '</p>';
include './HTML/Error.php';
exit();
}

我不知道错误是什么。有人可以帮助我吗?

最佳答案

这里:

$Statement = $MySQL->prepare($SQL);
   ^---your prepared statement
$Statement->bindValue(':username', $username);
$Statement->bindValue(':passwordhashed', $PasswordHashed);
$Statement->execute();
$Statement = $MySQL->query($SQL);
                        ^----raw queries have no placeholders

您准备一个语句并执行它。但随后您使用相同的 SQL 执行 RAW 查询,替换准备好的版本的结果。您不能在这样的原始查询中使用占位符。因此你的错误。

最后的 ->query() 调用是无用且多余的。

关于PHP 和 mysql 的 select 出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39208572/

相关文章:

mysql - 在 MySQL 查询中省略分号是不是很糟糕?

mysql - 进一步了解此查询

php - Laravel - 根据动态参数扩展 Eloquent where 子句

php - 如何在层次结构中设置第 3 个 li?

php - 使用 htaccess 重定向/重写

c# - 复杂查询的 Linq 内连接

ios - 无需重定向至 facebook.com 即可从手机分享 Facebook

c# - Intranet - Web 到远程 wcf CredentialCache.DefaultNetworkCredentials 不起作用

ruby - API认证和OAuth2的使用

php - cakephp 使用 hasAndBelongsToMany 进行分页查询