SQLSTATE[28000] [1045] Access denied for user 'root’#host - php

couldn't access database with error message:
SQLSTATE[28000] [1045] Access denied for user 'root’#‘xxx.ne.jp'(using password: YES)
I have this php code.
$dsn = 'mysql:dbname=mydb;host=xxx.ne.jp';
$user = 'root';
$password ='0123';
try{
$dbh = new PDO($dsn, $db_user, $db_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo $e->getMessage();
}
I can login to phpmyadmin with same username and password(root/0123).
why?
Do you have any idea to fix it?

Here is the basic PDO connection example:
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
So include the missing host name:
$dsn = 'mysql:host=localhost;dbname=mydb;host=xxx.ne.jp';
$user = 'root';
$password ='0123';
try{
$dbh = new PDO($dsn, $db_user, $db_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo $e->getMessage();
}
Change localhost if running site on another host.

Related

What is the issue in this PHP code to connect on localhost XAMPP

The error is:
Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'moomen'#'localhost' (using password: YES)
Code is:
<?php
$servername = "localhost";
$username = "moomen";
$password = "9124279123";
$dbname = "ecocaa";
try {
$conn = new PDO("mysql:host=$servername;dbname=$servername", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
I can see a problem in your code
In the dbname you have used server name, change the code it will work.

Does the PHP's define() not work with PDO connection string?

I was using the PHP's define() to define constants for my PDO connection string, ie. in mysqli. However, it just didn't seem to work. I kept getting the error: Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO) when I used the code below:
The PDO connection string would work when I didn't use PHP's define() function to pass in my connection variables. I'm using PHP 7, MySQL 8, and Apache 2.4.
Problem code below:
error_reporting(E_ALL); //check all type of errors
ini_set('display_errors', 1); // display those if any happen
//Database Connection Constant
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'root');
define('DB_NAME', 'gallery_db');
echo DB_PASS;
//phpinfo();
//$conn = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
try {
$dbc = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . "," . DB_USER, DB_PASS);
// set the PDO error mode to exception
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($dbc) {
echo "connected";
}
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Working Connection Code:
$servername = 'localhost';
$username = 'root';
$password = 'root';
$dbn = 'gallery_db';
//phpinfo();
try {
$dbc = new PDO("mysql:host=$servername;dbname=$dbn", $username, $password,
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => false
));
// set the PDO error mode to exception
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($dbc) {
echo "connected";
}
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Why does PHP define() not work in PDO's connection string?
In the first instance you try this:
$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.",". DB_USER, DB_PASS);
This only has two arguments, but PDO needs 3:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
?>
https://php.net/manual/en/pdo.connections.php
What happens is that your password constant works but went into the username part of the connection.
Fix:
$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
Change
$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.",". DB_USER, DB_PASS);
to
$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);

SQLSTATE[HY000] [1045] Access denied for user although the same user name password combination works with mysql_connect

I am using PDO to connect to MySQL database. The code works perfect when run in local host. However when I transfer the code to web server (of course I have changed the user name and password accordingly) the code does not work. It gives the following error
SQLSTATE[HY000] [1045] Access denied for user 'xxx_xxxxx'#'localhost' (using password: YES)
When I test connection using old deprecated API - mysql_connect() with same user name password combination, it works perfect, so I know that the user name password combination is right.
Here is my relevant code
try {
$dsn = "mysql:dbname=company_dbname; host=localhost";
$user = "company_dbuser";
$password = "MyPassword";
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC);
$conn = new PDO($dsn, $user, $password, $options);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Ultimately I am able to solve the problem
Here is my edited code
try {
$dsn = "mysql:dbname=company_dbname; host=localhost";
$user = 'company_dbuser';
$password = 'MyPassword';
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC);
$conn = new PDO($dsn, $user, $password, $options);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Please check $user and $password assignment - instead of double quote I assigned single quoted value.

PDO cannot connect to database

I'm having this problem where it says access denied, even though the login data is correct. I've tried multiple servers but it will keep saying access denied. The machine does have access to the server. I think it's my wamp that's not working.
Error:
Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'xml'#'localhost' (using password: YES)
Class:
class db
{
private $conn;
function __construct()
{
}
public function setupConnection()
{
if (!isset($conn)) {
$mysql_host = 'localhost';
$mysql_username = 'xml';
$mysql_password = '12345';
$mysql_db = 'xml';
try {
$conn = new PDO("mysql:host=" . $mysql_host . ";dbname=" . $mysql_db . "", $mysql_username, $mysql_password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
echo "Connectie is gefaald!: " . $e->getMessage();
}
return $conn;
}
}
}

Error: Access denied for user 'www-data'#'localhost'

I'm running a mysql database (managed through phpmyadmin) on a lubuntu virtual machine, which itself is on a mac 10.6.
Everything was working smoothly until earlier, (and I didn't touch any part of the login code!) when I got the following error on my pages:
Erreur : SQLSTATE[28000] [1045] Access denied for user 'www-data'#'localhost' (using password: NO)
I don't understand where this error is coming from as I don't have a user called www-data on phpmyadmin and my files are supposed to be using the root account on the database:
$dbname = 'name';
$dbuser = 'root';
$dbpass = '*****';
$dbhost = 'localhost';
try{
$linkpdo = new PDO('mysql:host='.$dbhost.';'.$dbname.', '.$dbuser.' , '.$dbpass);
$linkpdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (Exception $e) {
die('Erreur : '.$e->getMessage());
}
After some googling I saw that people modified their config.conf file, but I don't know how or why.
Use a double quoted string and the correct parameters for the connect, its easier to build using a double quoted string and $variable expansion.
$linkpdo = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$linkpdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
The user and password are separate parameters, http://php.net/manual/en/pdo.connections.php.
So take out the concatanation here-> .', '.$dbuser.' , '.$dbpass.
Connection should be:
$linkpdo = new PDO('mysql:host='.$dbhost.';dbname='. $dbname, $dbuser, $dbpass);
<?php
$dsn = 'mysql:dbname=name;host=localhost';
$user = 'root';
$password = '*****';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
Try this code. Checkout this link for more details http://php.net/manual/en/pdo.connections.php
Try this :
$linkpdo = new PDO('mysql:host='.$dbhost.';dbname='.$dbname , $dbuser, $dbpass);

Categories