1

When I try to access the database with my web application I get:

mysqli::mysqli(): (HY000/1045): Access denied for user 'php'@'localhost' (using password: YES) in C:\xampp\htdocs\Forum\accounts\PlayerAccount\Login.php on line 4 Error connecting to MySQL database (1045) Access denied for user 'php'@'localhost' (using password: YES)

I am not using root for the application. I made an user called PHP with all privileges. I am using xampp mysql database but it's still not working.

edit: on request here is the code:

<?php

  function getAccountRow($uuid){
  $configs = include('config.php');
  $mysqli = new mysqli($configs["host"], $configs["username"], $configs["password"], "account");    if($mysqli->connect_errno){
      die("Error connecting to MySQL database (".$mysqli->connect_errno.") ".$mysqli->connect_error);
    }
    $stmt = $mysqli->prepare("SELECT * FROM accounts WHERE uuid = ?");
    $stmt->bind_param("s", $uuid);
    $stmt->execute();
    $res = $stmt->get_result();
    $row = $res->fetch_assoc();
    return $row;
  }
    $file = fopen("test.txt","a");
    $post_json = file_get_contents("php://input");
    $post = json_decode($post_json, true);
    foreach($post as $key=> $value) {
        $message = $key . ":" . $value . "\n";
        file_put_contents("login_test", $message);
        fwrite($file,$message);
    }
    fclose($file);
    $response = array();
  $row = getAccountRow($post["Uuid"]);
  if($row["id"] == NULL){
$configs = include('config.php');
  $mysqli = new mysqli($configs["host"], $configs["username"], $configs["password"], "Account");    if($mysqli->connect_errno){
      die("Error connecting to MySQL database (".$mysqli->connect_errno.") ".$mysqli->connect_error);
    }
    //$_stmt = $mysqli->prepare("INSERT INTO accounts (uuid, name) values(?, ?)");
    //$_stmt->bind_param("ss", $post["Uuid"], $post["Name"]);
    //$_stmt->execute();
  }
  $row = getAccountRow($post["Uuid"]);
  $_rankperm = $row["rankPerm"];
  $rperm = false;
  if($_rankperm == 1){
    $rperm = true;
  }
  $response["AccountId"] = $row["id"];
  $response["Name"] = $row["name"];
  $response["Rank"] = $row["rank"];

  $response["RankPerm"] = $rperm;
  $response["RankExpire"] = (int) $row["rankExpire"];
  $response["EconomyBalance"] = 100;
  $response["LastLogin"] = (int) $row["lastLogin"];

    die(json_encode($response));
?>
reflective_mind
  • 1,475
  • 3
  • 15
  • 28
Lars Dormans
  • 171
  • 1
  • 13
  • Then at a guess you spelt the password wrong or you set the user up incorrectly or maybe you created a user called PHP and used a userid in the code of `php` – RiggsFolly Oct 05 '16 at 14:00
  • Did you flush privileges after you added the user? – Jay Blanchard Oct 05 '16 at 14:01
  • It seems you are not setting the root user to you connection, could you public the code that you use to connect? – Oscar Gallardo Oct 05 '16 at 14:01
  • 1
    @OscarDavid He said he was not using `root` its not a prerequisite to use `root` infact its one of those rookie mistakes people make – RiggsFolly Oct 05 '16 at 14:02
  • Make sure your username and password is correct. Reset it, if you have to. Make sure that the user/host combo has permissions for the `account` database, and that that database exists. – aynber Oct 05 '16 at 14:21
  • Note that @'localhost' permission has to be explicitly set for each user, and @'%' does not include it; MySQL considers them different permission areas. This was my most common permission problem with MySQL until I learned the hard way. https://stackoverflow.com/a/11634124/14744970 – SteveExdia Nov 08 '22 at 19:08

2 Answers2

2

Execute the following sentence via line command on your Mysql

> grant all privileges on your-database-name.* to 'php'@'localhost'  identified by 'your_pass_here';
> flush privileges;

It will give the permission to access to the user you're using to connect.

Oscar Gallardo
  • 2,240
  • 3
  • 27
  • 47
0

You Have problem with mysql auth. Try to connect to your db using Username: root, Password: '' (empty string). It is XAMPP defaults.

Rob
  • 127
  • 8
  • 1
    I know you are short of reps, but this is really just a comment. This type of answer attracts downvotes or gets flagged ___I did not DV___ and if that happens you will loose rep points and take longer to get to the 50 reps you need to comment on any question. Until then, stick to questions that are well asked and therefore easily answered without needing clarification. http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead – RiggsFolly Oct 05 '16 at 14:08
  • Thank you for your feedback)) I know that this type of answer is not good) but i can't write comments :D – Rob Oct 05 '16 at 14:17