Connect via PHP to MySQL Database created using cpanel - php

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.

Related

(HY000/1045) Access denied for user 'root'#'localhost'(using password: YES)

my localhost is saying error like Warning: mysqli::__construct(): (HY000/1045): Access denied for user 'root'#'localhost' (using password: YES) in C:\xampp\htdocs\registration\includes\db.php on line 3 on the browser I have all things done here's the image of the error in browser.
1: here is the image which is facing the error
and also if I am doing something wrong in my code editor then here's the image of the db.php db.php files code
This issue is tricky. But, how i went around it was straightforward.
in your mysqli_connect(_,_,_,_) arrange them in this order of "localhost", "user", "password", "database_name";
You may have an invalid password.
Edit: To me it looks like you're failing to actually pass parameters to the connection function, are you including / importing the variables?
You have passed invalid credentials (user name and/or password) when trying to connect to the database. What to pass can't be answered here because only you (the database administrator) would know about the existing user accounts.
To add an user account to a MySQL database see MySQL :: MySQL 5.7 Reference Manual :: 6.3.2 Adding User Accounts[^]. This requires connecting as root and providing the root password if one has been set.
The second is a result of the first and of using the wrong error function. For mysqli_connect(), you have to call PHP: mysqli::$connect_error - Manual[^] instead of mysqli_error() upon errors:
Hide Copy Code
$connection = mysqli_connect ($host, $user, $password) or die('Not connected : Ah sh*t ' . mysqli_connect_error());
change back your password to null
go to mysqladmin root (C:\xampp\mysql\bin) and type
mysqladmin --user=root --password=oldpassword password""
dont type any character when request new password, just push enter and enter
viola refresh your phpmyadmin page,
the problem caused by changing password via command window, just change the password via phpmyadmin page to avoid such error, http://localhost/phpmyadmin
This is how you should connect your database using mysqli :
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$db = new mysqli($servername, $username, $password, $dbname);
?>
I have had the same problem trying to install OpenCart on xampp (32bit). I was completing the information that I thought was being requested when in fact all I was being asked for was the name of the database. Leave all fields as oriinal just the name of your DB.
Leave password blank.
make sure this mapping is done
IN path\xampp\phpMyAdmin\congig,inc
/* Authentication type and info */
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = YOUR_DEFAULT_PASSWORD;
and you connection file
<?php
$servername = "localhost";
$username = "root";
$password = YOUR_DEFAULT_PASSWORD;
$db = "ecommerece";
$con = mysqli_connect($servername, $username, $password,$db);
your DB_HOST is your mysql server ip (most time is localhost)
your DB_USER is your User of Mysql (most time is root)
your DB_PASS is your Password of Mysql User (most time is none)
your DB_NAME is your name of your database in Mysql server

Access denied to mySQL database using PHP

So I've been trying to solve this problem for a couple of hours now. And I have to make something clear.
I can access the mySQL database from terminal using
mysql -u root
I can also access the database from mySQL workbench
My root user on mySQL has no password
Now my code online is pretty simple. It is PHP calling the mysqli
<?php
$database = "myDatabase";
// Create new connection
$conn = new mysqli("localhost", "root", "", $database);
if ($conn -> connect_error) {
die ("Something went wrong: " . $conn -> connect_error);
} else {
echo "It works";
}
?>
Now I always get the error code
Something went wrong: Access denied for user 'root'#'localhost' (using password: NO)
If I could get any help that would be greatly appreaciated. I've already tried to grant access but I think I'm doing it wrong.
mysqli_connect may be interpreting the empty password as no password at all
using password: NO
Either try to set a password for the root user, or create a new user with a password. And via Tripp Kinetics comment, make sure the permissions are set properly for the new user.

Does a PHP file that attempts to connect to a database have to exist on the same server as the DB?

For example, when trying to connect to server xy.xy.xyz.xyz like below, does the script have to exist on that server to work?
<?php
$servername = "xy.xy.xyz.xyz";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
No it doesn't. The DB server has to be configured to allow remote connections though.
You can check this for further details for MySQL.
No, what's most likely happening is that your MySQL database isn't allowing users to sign in if they are trying to access it from an outside IP address.
Run the following command through MySQL
SELECT *
FROM information_schema.user_privileges
WHERE GRANTEE LIKE '%username%';
Change the username value to whatever the actual user name value is. If you're seeing a bunch of rows where the GRANTEE column is 'username'#'localhost' and nothing that looks like 'username'#'%', then your user is being limited to only localhost (same machine) access.
If you want to grant your user non-local access, you can use the following:
GRANT ALL PRIVILEGES
ON mydatabasename.*
TO 'username'#'%'
IDENTIFIED BY 'mypassword'
In the above, % means my user can connect from anywhere. This example grants all administrator rights to your user, but you can change that if you wish.

Mysql database connection error

whenever I am trying to connect phpMyadmin through php script it is showing some errors like this Warning: mysql_connect(): Access denied for user 'www-data'#'localhost' (using password: NO) I am using ubuntu 11.04.So can you tell me how to solve this?
This error simply means that your connection information is incorrect. You are trying to connect to the MySQL database using the username "www-data#localhost" with no password. Check your MySQL permissions to see what you need to do. Either this login needs a password or this login is not specified as being permitted to access the data.
To check what permissions you have for that user, run this MySQL script:
SELECT * FROM user WHERE user='www-data';
To add rights to that user (if they are missing), run this script:
GRANT SELECT ON database.* TO 'www-data'#'localhost';
This works for me.
$dbhost = 'localhost';
$dbuser = 'www-data';
$dbpass = '<PUT PASSWORD FOR HERE>';
$dbname = '<YOUR DATABASE NAME>';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Could not connect: ' . mysql_error());
mysql_select_db($dbname);

How do I connect to mysql from php?

I'm working through examples from a book on php/mysql development.
I'm working on a linux/apache environment.
I've set up a database and a user. I attempt to connect with this line of code:
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
I get this error:
Warning: mysql_connect()
[function.mysql-connect]: Access
denied for user 'www-data'#'localhost'
(using password: YES) in
/var/www/hosts/dj/connect.php on line
3 unable to connect to database:
Access denied for user
'www-data'#'localhost' (using
password: YES)
I can only guess what is happening here:
I think www-data is a username for apache. Upon the database connection, the credentials being passed in to mysql are not those of my database user, but rather apache's own credentials. Is that what is happening here?
How do I pass in the credentials I've defined for my user ?
edit:
By the way - I do have credentials in the variables $db_hostname, $db_username, $db_password.
they are passed in by another file using require_once. If that file can't be found, then I get an error. So, I know that my username and password are being used by my script.
Both my scripts can be seen here:
http://pastebin.com/MUneLEib
#
Solved:
Thanks guys.
A couple of you pointed out that I had coded carelessy.
Also, I was particularly pleased by Neo's answer: he told me why the username of the owner of the apache process was being used.
:)
I just looked at your code! The variable with the username is $database_username but
you are using $db_username.. Change your code to:
$db_server = mysql_connect($db_hostname, $database_username, $db_password);
or you could change the line with username with:
$db_username='[your mysql user]';//or the username you created
When you don't pass anything it will be the user mysql assumes but it will not get the password so if you hadn't defined $db_password it would say: (using password:NO)
you set $database_username with you user but you are passing $db_username which is not set so the user is the linux username as default when nothing is passed with the password for the mysql user! Since there is no mysql user with that password or privileges or even with that name you are not given access!
That user is www-data which is as you guessed an apache user assigned to client-side requests!
In your login.php you use the variable $database_username, but in your connection function you use $db_username. Try matching them up.
The username goes into $db_username, and the password goes into $db_password.
All these other answers are so presumptuous, as if you don't know that $db_username means database username and same for password.
The error says that you've specified an username and password. You just specified the wrong ones. You need to use the username and password of MySQL, NOT the system username/password combination, so no, this will not be www-data. This may be root and some password, but again, these credentials are specified within MySQL, and are not (necessarily) the same as the system users and passwords.
Your MySQL installation should have a root user with a default password (which you should promptly change). There are several options: you can add an user via the MySQL command line or use an interface like cPanel or Webmin if your provider has something like this; I've used both of these and they both have easy interfaces to add new MySQL users and assign them privileges.
Also just a tip: I typically create one user per database and give the user full privileges on the database, and then use that user with the application linked to the database.
And then of course, once you create a MySQL user account and give it privileges on your web app's database, fill in that username and password into your script.
<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
?>
Make sure you actually assign your database login username and password to those variables before you try to connect. For example, before the line with mysql_connect():
$db_username = 'myuser';
$db_password = 'mypass123';
I'd recommend you go through a tutorial regarding MySQL and PHP before trying to go any further - just so you understand how it all works.
Try this one:
http://www.tizag.com/mysqlTutorial/mysqlconnection.php
Also, documentation is your friend:
http://php.net/manual/en/function.mysql-connect.php
The error given could mean that the privilege has not been granted, or perhaps the password is incorrect. Check that user is created and granted access, or just issue this to be sure it set to allow access on the mysql server.
$ mysql -u root -p
password:
(blah blah blah from server)
GRANT ALL PRIVILEGES ON db_base.* TO 'db username'#'localhost' IDENTIFIED BY 'some password';
If all privileges is too much, consider giving only the basic permissions needed:
GRANT SELECT,UPDATE,DELETE ON db_base.* TO 'db username'#'localhost' IDENTIFIED BY 'some password';
By the way, the server response to this statement on success is Query OK, 0 rows affected.
Your code is correct, it is saying your password is incorrect so your mysql database is rejecting the mysql connection. If you are using xampp use:
localhost
as hostname &
root
as username & in xampp there is no password so it would be
$password = ""; then $dblink = new mysqli($hostname, $dbuser, $password, $dbname);
so you can set your vars like this
<?php
$hostname = "localhost";
$dbuser = "root";
$password = ""; // means there is no password to the database
$dbname = "test"
?>
Conclusion :
if you get this as your message -
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'#'localhost' (using password: YES) in /var/www/hosts/dj/connect.php on line 3 unable to connect to database: Access denied for user 'www-data'#'localhost' (using password: YES)
It means the password you entered is wrong or there is no password.
Stop using mysql_connect()!
You should not use this or other related functions related to this extension.
It's depreciated and removed in PHP 7.0.0 and beyond.
An alternative is using PDO or MySQLi.
Below is an example script for connecting to MySQL using PDO:
<?php
/* Connect to a MySQL database using driver invocation */
/* DSN options:
http://php.net/manual/en/ref.pdo-mysql.connection.php
Error handling options:
http://php.net/manual/en/pdo.error-handling.php
Learn how to secure against SQL injection attacks:
http://php.net/manual/en/pdo.prepared-statements.php
*/
$user = 'myusername';
$pass = 'mypassword';
$host = 'localhost';
$mydb = 'mydatabase';
$dsn = "mysql:dbname=$mydb;host=$host";
try {
$dbh = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>

Categories