问题描述
当我尝试使用我的 Web 应用程序访问数据库时,我得到:
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在第 4 行连接到 MySQL 数据库时出错 (1045) 用户 'php'@'localhost' 访问被拒绝(使用密码:YES)
我没有为应用程序使用 root.我创建了一个拥有所有权限的名为 PHP 的用户.我正在使用 xampp mysql 数据库,但它仍然无法正常工作.
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));
?>
推荐答案
在你的 Mysql 上通过 line 命令执行以下语句
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.
这篇关于php xampp 访问被拒绝错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!