我知道这是一个新手错误,但我尝试了4天,我不能单独解决这个问题。
有人可以帮帮我吗?
<?php
class User {
private $dbHost = "HOST";
private $dbUsername = "USER";
private $dbPassword = "PASS";
private $dbName = "DB NAME";
private $userTbl = 'TABLE NAME';
function __construct(){
if(!isset($this->db)){
// Connect to the database
$conn = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName);
if($conn->connect_error){
die("Failed to connect with MySQL: " . $conn->connect_error);
}else{
$this->db = $conn;
}
}
}
function checkUser($userData = array()){
if(!empty($userData)){
// Check whether user data already exists in database
$prevQuery = "SELECT * FROM ".$this->userTbl." WHERE oauth_provider = '".$userData['oauth_provider']."' AND oauth_uid = '".$userData['oauth_uid']."'";
$prevResult = $this->db->query($prevQuery);
if($prevResult->num_rows > 0){
// Update user data if already exists
$query = "UPDATE ".$this->userTbl." SET first_name = '".$userData['first_name']."', last_name = '".$userData['last_name']."', email = '".$userData['email']."', gender = '".$userData['gender']."', locale = '".$userData['locale']."', picture = '".$userData['picture']."', link = '".$userData['link']."', modified = '".date("Y-m-d H:i:s")."' WHERE oauth_provider = '".$userData['oauth_provider']."' AND oauth_uid = '".$userData['oauth_uid']."'";
$update = $this->db->query($query);
}else{
// Insert user data
$query = "INSERT INTO ".$this->userTbl." SET oauth_provider = '".$userData['oauth_provider']."', oauth_uid = '".$userData['oauth_uid']."', first_name = '".$userData['first_name']."', last_name = '".$userData['last_name']."', email = '".$userData['email']."', gender = '".$userData['gender']."', locale = '".$userData['locale']."', picture = '".$userData['picture']."', link = '".$userData['link']."', created = '".date("Y-m-d H:i:s")."', modified = '".date("Y-m-d H:i:s")."'";
$insert = $this->db->query($query);
}
// Get user data from the database
$result = $this->db->query($prevQuery);
$userData = $result->fetch_assoc();
}
// Return user data
return $userData;
}
}
?>
显然代码是正确的,但是当我尝试记录它时会显示以下错误:
警告:mysqli :: mysqli():( HY000 / 1044):在/ var / www / html / URL / web / bbb / User中拒绝用户'USER'@'%'访问数据库'DB NAME'第12行的.php 无法与MySQL连接:用户'USER'@'%'访问被拒绝到数据库'DB NAME'
答案 0 :(得分:0)
确保您的用户有权在您的代码中执行您所查询的任何内容。 运行它到你的mysql
GRANT ALL on *.* to 'USER'@'%' identified by 'some_password'';
FLUSH PRIVILEGES;
答案 1 :(得分:0)
其中一个,您的凭据可能不是有效,另外它会导致 MYSQLi 处理(数据库,表格)名称包含空格<的问题/ strong>。