Database connection failing - php

I have a database connection file in my web application and for some reason the server is trying to connect to the location of: ***.**.**.215 for the ip address rather than: ***.**.**.222 which is what is typed in the file. The code is below:
<?php
// Configure the following items to match your database!
// Database Location
$location = '***.**.**.222';
// Database Name
$name = '';
// Database Username
$username = '';
// Database Password
$Password = '';
// Do not alter anything beyond this line!
// Create connection
$con=mysqli_connect($location,$username,$password,$name);
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Google Chrome shows this exact error message: I removed the credentials from it.
Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'username'#'***.**.**.215' (using password: NO) in /home/www/hiddenforsecurity/shop/config/connect.php on line 20
Failed to connect to MySQL: Access denied for user 'username'#'***.**.**.215' (using password: NO)
I'm not sure what in the world to do or what is wrong, when I replace $location in the $con with the actual ip address, it works perfect. It's just when I use the variable $location that it doesn't work.
I removed important private credentials from this question. Sorry hackers.

// Database Password
$Password = '';
// Create connection
$con=mysqli_connect($location,$username,$password,$name);
Change the capital $Password to $password.

You're on an client with ip address *.215 and you're trying to connect to MySQL server with ip address *.222, so make sure that the user '$username' on the MySQL server is allowed to connect from the ip *.215 you're using. (by default every user is allowed to connect only from local 127.0.0.1).
The connection you're trying to establish doesn't use password, notice that the variable you're using in the mysqli_connect(...) function is written $password and in the configuration above $Password (first letter upper case).
I suggest you to check the error log file (if you're using apache it is error.log inside the httpd folder), because in that file are logged all warning and errors and can help you solve problems, in this case probably there is something like this:
Variable $password undefined in page.php on line nnn.

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

Connect via PHP to MySQL Database created using cpanel

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.

Can't access database on my server

I am having some problems with my database. I created a user, and a database, but I can't seem to access it with php.
My code is:
$host = 'anapaiva.pt:2082';
$user = 'anapaiva_p1';
$pass = 'xxxx';
$db = 'anapaiva_mcmm1';
#mysql_connect($host, $user, $pass) or die('err: '.mysql_error());
#mysql_select_db($db) or die('err: '.mysql_error());
And on the webpage, appears the following errors:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'anapaiva_p1'#'apollo' (using password: YES) in /home/anapaiva/public_html/mcmm/connection/dbconn.php on line 6
Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/anapaiva/public_html/mcmm/connection/dbconn.php on line 7
The same code was working with my other server, hungergamesportugal.com, but I can't seem to upload the sql file to that database (it says I don't have permissions... I don't know why), and on this server it allows me to import the sql file, but I can't seem to access the database.
Can someone please help? :/
You're using the wrong port. 2082 is for cPanel. 3306 is for MySQL (usually).
Generally, you can omit the port entirely:
$host = 'anapaiva.pt';
You also need to assign the connect function to a variable:
$dh = mysql_connect($host, $user, $pass);
So that you can close the connection when you're done with it:
mysql_close($dh);
You can allow permission using cpanel, add permission to remote mysql. I don't remember exact name, but it was something like, remote database. on clicking you can add ip address to allow database permission. This may help you. search remote database at cpanel. May be this help you, http://forums.cpanel.net/f354/enable-remote-access-customer-database-251951.html
Test same user on localhost
$mysql_host = "localhost";
$mysql_database = "db_name";
$mysql_user = "root";
$mysql_password = "password";
$con = mysql_connect($mysql_host,$mysql_user, $mysql_password);
mysql_select_db($mysql_database, $con);
OR
Check user privileges
Using GRANT statement check whether user has rights.
Reference: http://dev.mysql.com/doc/refman/5.0/en/show-grants.html

Having trouble connecting to a MySQL database in php

I have a php file that holds my login details named "connect.php".
$host = 'localhost';
$username = 'username';
$password = 'password';
$database = 'database';
And I'm trying to connect with another file (html) with this as its contents:
$a = file_get_contents("http://example.com/folder/connect.php");
echo ($a);
mysql_connect ("$host", "$username", "$password");
#mysql_select_db($database) or die("Unable to select database");
And it keeps on giving me this error:
Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access
denied for user '**wrong username**'#'localhost' (using password: NO) in
/home/path/public_html/repo/path/downloads.php on line 40
Instead of
$a = file_get_contents("http://example.com/folder/connect.php");
echo ($a);
You should be using require/include:
require_once '/folder/connect.php';
Your current code still won't work using HTTP since you'd need to eval or parse the result to assign your $username and $password variables, just outputting the result of the PHP script doesn't assign any variables.
You shouldn't need to (or be able to) use http to download your MySQL connection information through HTTP especially with no authentication. That is a big security issue. If that is your setup you should change it, so that you have a local configuration file stored outside of your web root which has your db host, username, and password in it.
you should use include instead of file_get_contents in order to get the connect.php interpreted as a php file:
include '/path/to/connect.php';

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