【发布时间】:2017-02-14 00:28:30
【问题描述】:
当我尝试使用我的网络应用程序访问数据库时,我得到:
mysqli::mysqli(): (HY000/1045): 在 C:\xampp\htdocs\Forum\accounts\PlayerAccount\Login.php 中用户 'php'@'localhost' 的访问被拒绝(使用密码:YES)在第 4 行 连接到 MySQL 数据库时出错 (1045) 用户 'php'@'localhost' 的访问被拒绝(使用密码:YES)
我没有为应用程序使用 root。我创建了一个拥有所有权限的名为 PHP 的用户。我正在使用 xampp mysql 数据库,但它仍然无法正常工作。
编辑:根据要求,这里是代码:
<?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));
?>
【问题讨论】:
-
然后猜测你的密码拼写错误,或者你设置的用户不正确,或者你创建了一个名为 PHP 的用户并在
php的代码中使用了用户 ID -
添加用户后是否刷新权限?
-
你好像没有设置root用户来连接,你能公开你用来连接的代码吗?
-
@OscarDavid 他说他没有使用
root这不是使用root的先决条件,事实上这是人们犯的那些菜鸟错误之一 -
确保您的用户名和密码正确。重置它,如果你必须的话。确保用户/主机组合具有
account数据库的权限,并且该数据库存在。