SQLSTATE [HY000] [1044]当用户'@'localhost'上传到PhpMyAdmin数据库表时,访问被拒绝

时间:2019-02-14 13:39:13

标签: php mysql

首先,我对PHP相当陌生,因此我的代码中可能存在与该问题无关的错误(可能已在其他地方得到解答)。

问题:我正在尝试将准备好的语句上载到本地托管的PhpMyAdmin(XAMPP)中的表中,以创建用户帐户。每当我尝试创建新用户时,都会收到此消息...

Error: SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to 
database 'coursework,root'

我已经尝试了多种对其他人有用的解决方案,例如将我的服务器名从localhost更改为127.0.0.1并检查

$cfg['Servers'][$i]['AllowNoPassword'] = true;
我的config.inc.php和config.sample.inc文件的

都设置为true。但是,这并没有改变结果。

这是config.inc.php文件中的代码

$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['AllowNoPassword'] = true;
$cfg['Lang'] = '';

这是我的php文件的代码

<?php

$msg = "";
$servername = "localhost";
$username = "root";
$password = '';
$dbname = "coursework";

if (isset($_POST['RegisterBtn'])) {

    $Password = ($_POST['PasswordInput']);
    $CPassword = ($_POST['ConfirmPasswordInput']);

    if ($Password != $CPassword) {
        $msg = ("Passwords do not match, Please try again");
    } else {

        try {
            $conn = new PDO("mysql:host=$servername;dbname=$dbname,$username,$password");

            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            //Preparing the sql and binding new account parameters
            $statement = $conn->prepare("INSERT INTO userlogin (Forname,Surname,Email_Address,Password)VALUES(:Forname,:Surname,:Email_Address,:Password)");
            $statement->bindParam(':Forename', $Forename);
            $statement->bindParam(':Surname', $Surname);
            $statement->bindParam(':Email_Address', $EmailAddress);
            $statement->bindParam(':Password', $hash);

            //inserting user data
            $Forename = ($_POST['ForenameInput']);
            $Surname = ($_POST['SurnameInput']);
            $EmailAddress = ($_POST['EmailInput']);
            $hash = password_hash($Password, PASSWORD_BCRYPT);

            //executing the statement
            $statement->execute();
            $msg = "Your account has been created...";

        } catch (PDOException $e) {
            $msg = "Error: " . $e->getMessage();
        }
    }
}
$conn = null;
?>

任何有助于我理解的答案都会受到赞赏。谢谢!

1 个答案:

答案 0 :(得分:5)

这行是错误的。

$conn = new PDO("mysql:host=$servername;dbname=$dbname,$username,$password");

尝试一下。

$conn = new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);