Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.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
Php 警告:mysql_connect():拒绝用户“root”@“localhost”的访问(使用密码:是)_Php_Mysql - Fatal编程技术网

Php 警告:mysql_connect():拒绝用户“root”@“localhost”的访问(使用密码:是)

Php 警告:mysql_connect():拒绝用户“root”@“localhost”的访问(使用密码:是),php,mysql,Php,Mysql,警告:mysql\u connect:拒绝用户“root”@“localhost”的访问 在上的C:\xampp\htdocs\Login\sessionHandler.php中使用密码:YES 第35行 这就是第35行的内容 //to make a connection with database $conn = mysql_connect("localhost", "root", "password") or die(mysql_error()); //validation error f

警告:mysql\u connect:拒绝用户“root”@“localhost”的访问 在上的C:\xampp\htdocs\Login\sessionHandler.php中使用密码:YES 第35行

这就是第35行的内容

//to make a connection with database
$conn = mysql_connect("localhost", "root", "password") or die(mysql_error());
//validation error flag
$errflag = false;

//Input Validation
if($_POST['uname'] == '')
{
    $errmsg_arr[]='Login ID missing';
    $errflag = true;
}
if($_POST['pword'] == '')
{
    $errmsg_arr[] = 'Password missing';
    $errflag = true;
}


//if there are input validations, redirect back to the login form
if($errflag)
{
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: login.php");
    exit();
}

//to make a connection with database
$conn = mysql_connect("localhost", "root", "password") or die(mysql_error());


//to select the targeted database
mysql_select_db("ngchoonching", $conn) or die(mysql_error());

//to create a query to be executed in sql
$username = $_POST['uname'];
$password = $_POST['pword'];
$query = "SELECT * FROM profile WHERE username = '$username' AND password = '$password'";

//to run sql query in database
$result= mysql_query($query, $conn) or die(mysql_error());

//check whether the query was successful or not
if(isset($result))
{
    if(mysql_num_rows($result) == 1)
    {
        //Login successful
        session_regenerate_id();
        $member = mysql_fetch_assoc($result);
        $_SESSION['SESS_MEMBER_ID'] = $member['id'];
        $_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
        $_SESSION['SESS_LAST_NAME'] = $member['lastname'];
        $_SESSION['STATUS'] = true;
        session_write_close();
        header("location: login-successful.php");
        exit();
    }
    else
    {
        //login failed
        header("location: login-failed.html");
        exit();
    }
}
else
{
    die("Query failed");
}

?>
我不知道这是问题还是我正在写的代码。 我已经寻找了每一个可能的答案,但它不是我一直在寻找的

如果问题不在第35行,这是我的代码
//validation error flag
$errflag = false;

//Input Validation
if($_POST['uname'] == '')
{
    $errmsg_arr[]='Login ID missing';
    $errflag = true;
}
if($_POST['pword'] == '')
{
    $errmsg_arr[] = 'Password missing';
    $errflag = true;
}


//if there are input validations, redirect back to the login form
if($errflag)
{
    $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
    session_write_close();
    header("location: login.php");
    exit();
}

//to make a connection with database
$conn = mysql_connect("localhost", "root", "password") or die(mysql_error());


//to select the targeted database
mysql_select_db("ngchoonching", $conn) or die(mysql_error());

//to create a query to be executed in sql
$username = $_POST['uname'];
$password = $_POST['pword'];
$query = "SELECT * FROM profile WHERE username = '$username' AND password = '$password'";

//to run sql query in database
$result= mysql_query($query, $conn) or die(mysql_error());

//check whether the query was successful or not
if(isset($result))
{
    if(mysql_num_rows($result) == 1)
    {
        //Login successful
        session_regenerate_id();
        $member = mysql_fetch_assoc($result);
        $_SESSION['SESS_MEMBER_ID'] = $member['id'];
        $_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
        $_SESSION['SESS_LAST_NAME'] = $member['lastname'];
        $_SESSION['STATUS'] = true;
        session_write_close();
        header("location: login-successful.php");
        exit();
    }
    else
    {
        //login failed
        header("location: login-failed.html");
        exit();
    }
}
else
{
    die("Query failed");
}

?>

我使用localhost、phpmyadmin、xampp,我不明白为什么访问被拒绝。

试试$conn=mysql\u connectlocalhost,root或$conn=mysql\u connectlocalhost,root,

这在代码中不是问题,这是数据库权限的问题。显示补助金的用途是什么root@localhost; 显示?同时避免使用mysql或更高版本PDO@Barmar它说,使用GRANT选项将@上的代理授予“root”@“localhost”,并使用GRANT选项将**上的所有权限授予“root”@“localhost”。使用当前代码可以执行简单的sql注入,只需从表中返回一行,并使用该行进行身份验证,甚至可以作为登录名任何人将mysqli或PDO用于准备好的查询's@Barmar对不起,我该怎么办?这实际上是我第一次这样做,我仍然在学习它真的有效!非常感谢。但是为什么我必须删除密码呢?我只是想知道这有什么区别,因为你没有设置根密码。换句话说,您的SQL设置非常非常容易受到攻击。有很多关于保护它的开始提示可能是访问权限问题。。。请尝试从mysql.users中选择*以查看拥有密码的用户。对于root用户,在您的情况下,密码可能是空的。。。如果您以后在开发过程中设置密码,只需将password参数带回来。@Rogue听起来很糟糕,谢谢您提供的信息@onedevteam.com非常感谢您!这对我帮助很大