I'll try my best to explain the process hoped to achieve from the code and the error occurrence.
First, I am creating a database for each member who joins and wishes to store information. So I have the first connection to check for session log in and store the username, at which point I end the connection. The next connection goes into the root to create a user and database in SQL. After that, i try the third connection and receive the following error
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'DB_USERNAME'#'localhost' (using password: YES) in C:\xampp\htdocs\managementportal.php on line 38
ERROR: Could not connect. Access denied for user 'DB_USERNAME'#'localhost' (using password: YES)
The sql query does not seem to be working in a way with which is creates a user/database and therefore there is nothing to connect which automatically gives me an error to connect.
PHP Code
<?php
$link = mysqli_connect("localhost", "php", "test", "php");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
session_start();
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: members.php");
exit;
}
$username = trim($_SESSION['username']);
mysqli_close($link);
$slink = mysqli_connect('localhost', 'root', '');
define('DB_SERVER', 'localhost');
define('DB_USERNAME', '$username');
define('DB_PASSWORD', 'test');
define('DB_NAME', '$username');
$createdb = "CREATE USER IF NOT EXISTS 'DB_USERNAME'#'localhost' IDENTIFIED VIA mysql_native_password using 'DB_PASSWORD';
GRANT ALL PRIVELGES ON *.* TO 'DB_USERNAME'#'localhost'
REQUIRE NONE WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR0 MAX_USER_CONNECTIONS 0;
CREATE DATABASE IF NOT EXISTS 'DB_NAME'; GRANT ALL PRIVILEGES ON 'DB_USERNAME' *.* TO 'DB_USERNAME'#'localhost'; " ;
mysqli_query($slink, $createdb);
mysqli_close($slink);
$ulink = mysqli_connect('localhost', 'DB_USERNAME', 'DB_PASSWORD', 'DB_NAME');
if($ulink === false){
die("ERROR: Could not connect. " . mysqli_connect_error($ulink));
}
?>
Related
This question already has answers here:
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'#'localhost' (using password: YES)
(25 answers)
Closed 2 years ago.
Here is my code which I am using to connect my database. My credentials are perfectly working when directly login with MySQL console but the same is not working when trying to connect DB from my PHP code.
Error ;PHP Warning: mysqli_connect(): (HY000/1045): Access denied for user
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$servername = 'localhost';
$db_name = 'HappaningsDb';
$username = 'SIDSAHU3';
$password = 'sid';
$conn =null;
//$link = new PDO("mysql:host=$servername;dbname=$db_name", $username, $password);
// $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$link = mysqli_connect($servername, $username, $password, $db_name);
// Check connection
if($link == false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt insert query execution
$sql = "insert into Question_data(QUESTION_ENG,OP_ENG, TOPIC_ID, CAT_ID, IS_PREMIUM, IS_HIN,
QUESTION_HIN ,OP_HIN, ANSWER, CREATE_USER,CREATE_DT,UPDATE_USER, UPDATE_DT)values('hsdvhbsvsvk',
'dhvjd','1','1','N','N','','','2','sid',curdate(),'','');";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
Please find attached screenshot:
User SIDSAHU3 could be restricted to Local host
From your CLI, try creating user with %
CREATE USER 'myuser'#'%' IDENTIFIED BY 'mypass';
GRANT ALL ON *.* TO 'myuser'#'%';
I'm trying to connect my PHP to my XAMPP DB server, but as it seems I'm not doing something right.
This is my code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$db= "blogdata";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
And it returns:
Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'username'#'localhost' (using password: YES) in C:\xampp\htdocs\KungFu\Blog.php on line 8
Connection failed: Access denied for user 'username'#'localhost' (using password: YES)
I think everything is correct, tried different variations but as you can tell it is not working.
Best way to connect php to MySQL is to use new method : PDO :)
PHP :
$bdd = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '');
I tried using the following code in php:
<?php
$servername = "www.psgcirdask.com";
$username = "xxx"; //hidden for security purpose
$password = "password";
$dbname = "psgcilqh_calibration";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM brands";
$result = $conn->query($sql);
$conn->close();
?>
I got an error:
Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'xxx'#'157.51.147.43' (using password: YES) in C:\xampp\htdocs\justshawarmapos\checkthedatabase.php on line 9
Connection failed: Access denied for user 'xxx'#'157.51.147.43' (using password: YES)
But i use the same username and password to connect to my database in my website.I am getting this problem only when i connect using local server.
You have to enable remote access in your mysql database.
Check this link
For hostgator
Check if all the infos provided are correct especially the servername then try to figure out if you have the right permission to access that database with that user!
I create a database called 'demo'.
I set a username called 'username'.
I set a password called 'password'.
I connect to the database.
I run the following:
<?php
define ('DB_NAME', 'demo');
define ('DB_USER', 'username');
define ('DB_PASSWORD', 'password');
define ('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('Can\'t use ' . DB_NAME . ':' . mysql_error());
}
echo 'Connected successfully';
?>
And I get the following errors.
Warning: mysql_connect(): Access denied for user 'username'#'localhost' (using password: YES) in C:\xampp\htdocs\PhpProject2\index.php on line 101
Could not connect: Access denied for user 'username'#'localhost' (using password: YES)
I am beyond confused. I'm using the correct username and password, but still it doesn't connect, any ideas where the problem might lie?
Are you sure that this credentials are correct? Alternatively, have this user permissions to do this?
If your user hasn't a password see this
SET PASSWORD [FOR user] = password_option
password_option: {
PASSWORD('auth_string')
| OLD_PASSWORD('auth_string')
| 'hash_string'
}
Example
SET PASSWORD FOR 'username'#'localhost' = PASSWORD('password');
Permission can be set with Grant
GRANT ALL ON demo.* TO 'username'#'localhost';
You may have to give GRANT permissions to that user on MySQL database
I have an application written in C# and it does connect with the database.
Here's the error I'm getting:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'username'#'localhost' (using password: YES) in (php file) on line 15
Could not connect: Access denied for user 'username'#'localhost' (using password: YES)
Here is my code:
$mysql = mysql_connect("localhost", "username", "password");
if (!$mysql) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
I got this example from:
http://php.net/manual/en/function.mysql-connect.php
What am I doing wrong?
Your username in mysql in probably not "username", as your password is not "password". Please change it to something like
$mysql = mysql_connect("localhost", $username, $password);
where $username is variable with your db username and $password is variable with your db password.
Also it would be good thing to use MySQLi instead of mysql_ functions.