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 ?
Related
This question already has an answer here:
Why cannot the PHP hide the error message thrown by the MySQL connection?
(1 answer)
Closed last month.
I'm trying to perform a connection between PHP and a MySQL database via Mysqli. I have the following code:
<?php
$database_host = "localhost";
$database_username = "root";
$database_password = "";
$database_name = "test3";
$connection = new mysqli ($database_host, $database_username, $database_password, $database_name);
if ($connection->connect_errno) {
echo ("Can't connect to database because $connection->connect_error"); }
else {
echo ("Connection was successful"); }
?>
I'm using this code for a setup wizard for the users to install a software in their webservers. The point is that if the credentials are correct it prints "Connection was successful", but if wrong its output is:
**Fatal error**: Uncaught mysqli_sql_exception: Access denied for user 'root'#'localhost' (using password: YES) in /opt/lampp/htdocs/uuid/index.php:55
Stack trace:
#0 /opt/lampp/htdocs/uuid/index.php(55): mysqli->__construct('localhost', 'root', '', 'test3')
#1 {main}
thrown in **/opt/lampp/htdocs/uuid/index.php** on line **55**
I currently have error reporting for PHP on (without it, I couldn't get this error message), but regular users who upload the software to a web hosting and they input incorrect credentials will receive a blank page instead.
So how can I print "Can't connect to database because [...]", so users know why is this error being thrown?
In PHP 8.1.0, MySQLi sets the report mode to MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT by default. This causes MySQLi errors to throw exceptions.
So catch the exception and report the error how you want:
try {
$connection = new mysqli ($database_host, $database_username, $database_password, $database_name);
} catch (mysqli_sql_exception $e) {
echo "Can't connect to database because $e";
... return appropriate http status code ...
}
echo "Connection was successful";
Note: The above is just a quick example of handling an exception. Error handling and reporting is a more complex topic, and this is not meant to show the best practice for every application case.
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 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 :).
I have a database and database user created via cpanel as below:
database name: test_database
database user: test_user
I am trying to connect to this database on the same server via a php script below:
<?php
$servername = "localhost";
$username = "test_user_test_database";
$password = "test";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
However I end up with:
Warning: mysqli_connect(): (28000/1045): Access denied for user 'test_user_test_database'#'localhost' (using password: YES) in /home/driver/public_html/login/connect.php on line 7
Connection failed: Access denied for user 'test_user_test_database'#'localhost' (using password: YES)
Is the issue down to having an underscore in the username and database name? Or is there a way round this? I would much rather keep the username and database name as they are so wanted to check if there is any other way round it
Underscores are fine for naming.
mysqli_connect takes the database name as the 4th argument. Try adding your database name.
Finally, I seem to remember cPanel using some sort of automatic prefixes or something... double-check your database credentials if it's still not working.
You'll also have to add the user and the user permissions on that database.
The code is:
$mysql = new mysqli('10.1.36.20', 'user', 'password', 'dbname');
The result is:
Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'user'#'10.1.36.111' (using password: YES) in ....
webserver IP: 10.1.36.13;
mysql server IP: 10.1.36.20
where did it got 10.1.36.111?
I was tried through mysql_connect - The same problem.
The fastest fix is to look at the error and read it. For example, if "(using password: NO)", then you need to make sure your connection is using a password.
If "(using password: YES)", then make sure you are using the correct password.
If the error reports
Couldn't connect :
localhost: Connection error to server 'localhost' with user 'username_user'
Make sure you have added the user to the database and granted permissions to this user.
or
Create a new user or use proper username and password for the database
for managing this go to your Phpmyadmin->User->Add user
Default:
username:"root"
password:""
And use this function for reference.
<?php
// db connect to nm database
function db_connect_nm()
{
//Database server
$host= 'localhost';
$nm_name= 'myname_databasename';
$nm_user= 'myname_dbusername';
$nm_pword= 'password';
$nm_connect = new mysqli($host, $nm_user, $nm_pword, $nm_name);
if (!$nm_connect)
throw new Exception('Could not connect to NM database ');
else
return $nm_connect;
}
?>
9 out of 10 PHP problems can be resolved by setting so please check that too