I recently installed the latest version of XAMPP and transferred my database over to it. I created my user accounts on phpmyadmin, however when i try to access the database with any user other than root through PHP I get:
Connection failed: SQLSTATE[HY000] [1045] Access denied for user
It does not work for both users with and without a password. I have tried flushing the privileges deleting and creating the users again but nothing seems to work.
Example connection code:
<?php
$dsn = 'mysql:dbname=test_db;host=127.0.0.1';
$user = 'test_user';
$password = 'test';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
If you are SURE of your valid credential, it's maybe a charset problem ( due to your database transfert)
Try to go in phpmyadmin, and change the collation on operations tab to "t8_general_ci".
But, in most of time, your error is due to bad credential or bad privileges.
Check it too... But first :).
Related
I'm using Jetbrains and Mysql to work on this practical project, but when I connect to the mysql
database it gives me the following error:
C:\wamp64\bin\php\php5.6.40\php.exe C:\wamp64\www\Social_Network\Includes\connection.php
Connection failed: SQLSTATE[HY000] [1049] Unknown database 'social_network'
Process finished with exit code 0
I made sure several times that the database name is the same name and there are
no spelling errors at all (I copy pasted it from the database)
Here's my code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=social_network", $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();
}
//?>
Welll, there's little that can be done about it. MySQL thinks that the database does not exist.
is the server the correct one?
is the case sensitivity set correctly? "Social_Network" and "social_network" might be considered different.
can you access the database with those parameters using a different tool (e.g. HeidiSQL, SQLYog, SQLterm, in a pinch even phpMyAdmin)?
Actually, JetBrains PHPStorm has a SQL terminal utility that can diagnose the connection. You may want to use it (once it knows what database you're connecting to, it will also warn you of several possible errors such as using the wrong table name or column name).
I have one file on one web serve and my phpmyadmin SQL databases on a seperate server and am currently trying to connect to my external serve but it doesnt seem to work.
I keep getting the error message:
'Access denied for user ''#'localhost' (using password: NO)'
and am not sure what this means.
Any help would be greatly appreciated or even an article to get started on figuring this out since I cant seem to find anything myself.
Thank you
EDIT --- PDO SCRIPT ---
My PDO Script is the generic one from w3, I have place holders for security reasons.
<?php
$servername = "externalIP";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=Reservations", $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();
}
?>
you have to find your phpmyadmin page where you login using also a password.
Try this one :if you have not assign password into database for specific user then you just have to apply 'root' as user and leave blank password field and also set your hostname as 'localhost' or 127.0.0.1
also remember that you have to start first apache and mysql services before access.
If you are using remote server as database then not required.in that case you have to apply your remote Ip address and also remember that in that remote server if you have not specific user then leave password as blank. one another way to debug mysql connection into your remote server creating mysql connection file with its required paramter to check that your remote server ok or not.
This error will come most probably because of user permission issue. Make sure you have privilege to that user for that database.
I have recently deployed my new website, I use GoDaddy Unlimited Hosting and I'am unable to connect to the database for some reason.
Here's my code for connecting my website to the database:
<?php
ob_start();
session_start();
// db properties
define('DBHOST','some.example.ip.Address');
define('DBUSER','username');
define('DBPASS','password');
define('DBNAME','database-name');
$conn = #mysql_connect (DBHOST, DBUSER, DBPASS);
$conn = #mysql_select_db (DBNAME);
if(!$conn) {
die('Some Error Message');
}
define('included', 1);
?>
And Instead of connecting my website to the database and showing the content It shows the die() error message I have used above, And I tried adding if (! $conn) { mysql_error() or die('some message'); } I can see my website but can't see the db content and when I submit any form It shows this message Query failed. Access denied for user 'root'#'localhost' (using password: NO)
Thank you for your time.
Look into using PDO. mysql_connect is deprecated and generally not considered secure. However, it should still work.
Here's a simple PDO connection:
TRY {
$handler = new PDO('mysql:host=localhost;dbname=NameofDB','username','password');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
CATCH(PDOEXCEPTION $e) {
echo $e->getMessage() . "<br>";
die ('sorry for your luck!');
}
There are lots of tutorials around the web for PDO implementation, and it's generally considered he way to go.
I keep getting a connection error when trying to connect to my database.
I am on a shared host for my webpages.
I started by creating a database: ('herman' being replaced by the prefix added by my host)
Username = herman_tst
Password =test
Name = herman_tst
I can get into it via phpmyadmin without any problem as I imported some tables into it (not containing any user parameters)
I uploaded my .php files like the index.php etc
I updated my dbConnect.php file that makes the database connection (I believe) accordingly ('herman' being replaced by the prefix added by my host)
$dbUsername = "herman_tst";
$dbPassword = „test”;
$dbName = "herman_tst";
try {
$conn = new PDO("mysql:host=localhost;dbname=$dbName",
$dbUsername,
$dbPassword);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
print 'Error: ' . $e->getMessage();
}
Still my browser respond that access to the database is denied:
Error: SQLSTATE[28000] [1045] Access denied for user
'deb41858_tst'#'localhost' (using password: YES) Fatal error: Call to
a member function prepare() on a non-object in
/home/deb41858/domains/doordelens.be/public_html/php/index.php on line
16
Cn anyone please indicate what I am missing here ?
try to set host in dsn of pdo like this:
<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=xxx_online;host=192.168.1.105;';
$user = 'username';
$password = 'password';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
it works now. I misunderstood it.
The error means your username and/or password isn't correct, (or isn't actually set up on your server). The reason it works on your other server is because that user exists there.
You either need to check you have the right username and password set in your PHP file, or you need to create a new MySQL user, and connect with the new username and password for that server.
http://dev.mysql.com/doc/refman/5.1/en/adding-users.html
If you're uncomfortable doing this from the command line, you can install phpMyAdmin via apt-get and do it through a web interface.