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));
?>
6
  • 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 Commented Oct 5, 2016 at 14:00
  • Did you flush privileges after you added the user? Commented Oct 5, 2016 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? Commented Oct 5, 2016 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 Commented Oct 5, 2016 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. Commented Oct 5, 2016 at 14:21

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Nowadays its might be useful to add ALTER USER 'php'@'localhost' PASSWORD EXPIRE NEVER Or in 30 days it might well stop working again with a comment about recent mysql 5.7 changes
Granting everything to all databases is a huge security hole. It should only be set to the needed databases.
Hear Hear @aynber
How do i do that using phpmyadmin
If you have root user, just execute as SQL, but if you are using a shared service, you could try to give permission to the user via wizard
|
0

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

2 Comments

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. meta.stackexchange.com/questions/214173/…
Thank you for your feedback)) I know that this type of answer is not good) but i can't write comments :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.