用户拒绝访问

时间:2016-07-20 11:44:06

标签: php mysql access

我继续收到此错误:

  

无法连接到MySQL:用户“root”@“localhost”拒绝访问(使用密码:XXX)

我正在使用mamp服务器来运行我的Web服务器。为什么我会收到此错误?一切看起来都不错,但它让我无法访问。

这是我的代码:

<?php
$servername = "localhost";
$dbname = "dbtechnerdzzz";
$user = "root";
$password = "";
session_start(); // Starting Session
$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
  if (empty($_POST['form-username']) || empty($_POST['form-password'])) {
    $error = "Username or Password is invalid";
  }
  else
  {
    $connection=mysqli_connect($servername, $user, $password, $dbname);
    // Check connection
    if (mysqli_connect_errno()){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    // To protect MySQL injection for Security purpose
    $username = mysqli_real_escape_string($connection,$_POST['form-username']);
    $password = mysqli_real_escape_string($connection,$_POST['form-password']);
    // SQL query to fetch information of registerd users and finds user match.
    $query = mysqli_query($connection,"select * from accounts where Password='$password' AND Username='$username'");
    $rows = mysqli_num_rows($query);
    if ($rows == 1) {
      $_SESSION['login_user'] = $username; // Initializing Session
      header("location: profile.php"); // Redirecting To profile Page
    } else {
      $error = "Username or Password is invalid";
    }
    mysqli_close($connection); // Closing Connection
    header("location: signin.php"); // Redirecting To login Page
  }
}
?>

1 个答案:

答案 0 :(得分:4)

mamp的默认密码是 root 。 像这样改变你的配置。

$servername = "localhost";
$dbname = "dbtechnerdzzz";
$user = "root";
$password = "root";