Could not connect: Access denied for user php - php

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.

Related

Connecting PHP with MySQL DB in XAMPP issues

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', '');

SQL Database Creation Error in PHP (Multiple SQL Connections)

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));
}
?>

000webhost mysql connection error

I am trying to connect to my database using the following code:
<?php
$mysql_host = "mysql*.000webhost.com";
$mysql_database = "a******_account";
$mysql_user = "a******_admin";
$mysql_password = "******";
// Create connection
$con=mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database);
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
};
?>
Does anyone know what is going on?
The error messages are:
Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'a******_admin'#'0.0.0.0' (using password: YES) in /home/a3996154/public_html/index.php on line 18
and
Failed to connect to MySQL: Access denied for user 'a******_admin'#'0.0.0.0' (using password: YES)
Probably: Your auth is wrong, or that user doesn't have permission to access to that database.
Sorry, it took a day for the database to respond, today it is up and running. Excuse me for any time I wasted.

Correct PHP credentials for database leads to error signifying wrong credentials

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

Could not connect: Access denied for user 'indysoft_admin'#'localhost

ii have made a Mysql database and and user for the same with all permissions Still am getting the following error.
Could not connect: Access denied for user 'indysoft_admin'#'localhost
i am using the following connection.php script for connection
<?php
$con = mysql_connect('localhost', 'indysoft_admin','');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else{
mysql_select_db("indysoft_person", $con);
}
?>
You didnt create the User with the same credentials, like your passing. Check your Database for ur username and password. Maybe a misspelling?

Categories