<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
This is the code I am using to check if I am able to connect to Mysql.
I am having problem connecting using this code. Should I change localhost to the name of the website?
I tried ("www.abc.com","login username", "password") even this is not working.
EDIT:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'mobile4_you'#'cl79.blahblah.com' (using password: YES) in /home/mobilew4/public_html/mysql.php on line 2 Access denied for user 'mobile4_you'#'cl79.blahblah.com' (using password: YES)
If you're using something like cPanel, it will prefix your username and your database with your username for cpanel, for example:
> cPanel login: foo
> Database User: bar
> Database: ey
Your database user will be foo_bar and your database will be called foo_ey
Localhost refers to where the database is running. If it's running on the same server as your PHP server localhost should work fine. What kind of problems are you having? Are you sure that your username/password is correct?
You should replace :
localhost by the name of the server on which your MySQL server is
if your MySQL server is on the same server as PHP, then it's localhost
username by the login that allows you to access your database
password by the corresponding password
Of course, this is supposing you actually have a MySQL server up and running, with a user account created on it.
Didn't you hosting company provide you with any information related to MySQL ? Using those informations, what error message does mysql_error() give ?
Do you have a mysql user which you've set up? You'll need to do that. In cPanel you can do it by clicking on MySQL databases, and on phpMyAdmin you can just add a new user on the start page. Put that users' username and password in.
You need to pay attention to the error, if any, reported by mysql_error(). This should give you some information you can use to diagnose the connection problem.
See this MySQL Forge wiki page about connection failure:
http://forge.mysql.com/wiki/Error2003-CantConnectToMySQLServer
Or this page from the MySQL Manual:
http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html
Related
I am trying to view my website from godaddy. The domain name is registered on somewhere else and hosting is from godaddy. After changing DNS settings, I cretae my db in godaddy and import all tables in it. After that, i created a username and give it to all previleges. Then, when i tried to connect to database, i am facing error
Access denied for user 'myusername'#'ip-166-62-28-82.ip.secureserver.net' (using password: YES)
db.php
<?php
$conn = mysql_connect("166.62.28.82", "myusername","mypassword", "arikai2_2" ) or die(mysql_error());
mysql_select_db("arikai2_2");
?>
Where did you get the dbhost value from?
In your hosting panel, there should be somewhere a section where you can get the right details about connecting to your database server and its databases.
If you are using the classic goDaddy hosting, you can find this info in the:
Databases > MySQL Section > Actions > Details
The dbhost is down at the bottom of this screen: Hostname
Make sure that the mysql host is correct. You can contact your hosting provider to help you getting the proper host for your connection string.
Anyone having issue even after correct username,password and database name, reset your database user's password and update it in your mysqli connection code. This is weird but it worked for me.
Recently i have purchased a domain "domain.com". The hosting company has the usual CPanel to handle databases.
Using the "MySQL Databases" I:
Created a database with the name "database1"
Created a user with the username "user1" and password "pass1"
Added user "user1" to the database "database1"
So far so good.
After that i clicked on PHPMyAdmin and redirected to the PHPMyAdmin webpage. There i see the "database1" that i have previously created. Clicking on that database, i used IMPORT to import a very simple table named "test" [columns (id,name,surname)]. Importing the table created table "test" below the database "database1", which is correct.
After that i have tried to connect to that database using the code below (connect_to_db.php):
// Create connection
$con=mysqli_connect("domain.com","user1","pass1","database1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error() ."<br>";
}
I have uploaded the connect_to_db.php into the /home/domain/public_html/.
When i try to connect to the database1 i get the following error:
Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'user1'#'xxx.xxx.xxx.xxx' (using password: YES) in /home/karkoona/public_html/connect_to_db.php on line 13
Where xxx.xxx.xxx.xxx is the public IP of domain.com (if i get it right).
Also on the right of landing page of PHPMyAdmin i see:
user: domain#localhost
Any idea why i cannot access the database with the current user?
Is there any mistake in my code?
Thanks.
You need to grant access to user1 using it's IP to access the remote server:
GRANT ALL PRIVILEGES ON database1.* to 'user1'#'xxx.xxx.xxx.xxx' IDENTIFIED BY 'pass1';
NOTE: replace the relevant info to the current info such as xxx.xxx.xxx.xxx to the current server IP that will access the remote MySQL.
Since you're using cPanel you can do this to allow the IP:
Login to your cPanel (if not already logged in).
Scroll down to the Databases section.
Click on the Remote MySQL icon.
Enter the IP address of the computer that will be making the remote connection.
Click the Add Host button.
It may be beacuse :
1) User name or password you are using is wrong
2) User may not have privillage to access the database
you are not pass $con in connection check
if (mysqli_connect_errno())
replace it with
if (mysqli_connect_errno($con))
All you need to do is to add your server IP address to Remote Database Access Hosts in cpanel. It will allow access to your database and the same method is used to allow access when you are using localhost. But all you have to do is to add your computer IP address into remote database.
I have a website that connects to a database and retrieves page content and all works fine. However, I then use the same database connection to connect to and retrieve user details for logging in to my site (2 tables but both in same database). However, I get the following error message when doing this.
Warning: mysql_query() [function.mysql-query]: Access denied for user 'username'#'localhost' (using password: NO)
The hint is in "using password NO" There is no password being sent - in fact it is only using username#localhost as the username so it looks like you don't have your credentials configured properly.
Check config.inc.php - http://wiki.phpmyadmin.net/pma/Configuration_storage
Well it's says that your password is not correct and that you haven't gained access. Can you please upload a code here ? if you're using localhost, then the username should be root .. try to change it and leave the password field empty for now.
I've tried to search for an existing answer to this problem, but the answers I find have not worked thus far.
I've been attempting to use PHP to connect to a MySql database. My web host uses cPanel on Linux. The code I'm using to do this seems standard enough:
$mysqli = new mysqli("localhost", "cPanelUsername_dbUsername", "dbPassword", "cPanelUsername_dbName");
I've been getting the following error:
Failed to connect to MySQL: (1045) Access denied for user 'cPanelUsername_dbUsername'#'localhost' (using password: YES)Access denied for user 'cPanelUsername'#'localhost' (using password: NO)
"localhost" is the host server where the MySql server is located (it seems like this works)
"cPanelUsername" is my cpanel username
"dbUsername" is the database user, which I added to the database with all permissions granted
"dbPassword" is the database password for dbUsername
"dbName" is the database name
I ended up adding my cPanel username before the dbName and dbUsername after searching for answers to this issue elsewhere.
It looks like I have everything set up correctly but it's not connecting (with the error above). I don't have any direct control over the server that I wouldn't have to ask my web host about, which may take a few days to get sorted out. Do I have something wrong with my connection code?
First check the database that you gave the proper user access to your database, which is given from Add User to databases from Mysql database section in cpanel.
after that check it again,
first try normal connection code in php,
$con = mysql_connect("localhost","cpanel_username","cpanel_password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
In cPanel, make sure that:
The database user cPanelUsername_dbName exists, with the password dbPassword
The database you want to use exists.
The user cPanelUsername_dbName is allowed to access the database.
The user cPanelUsername_dbName is allowed to access the database from localhost, 127.0.0.1, and the IP address of your server.
Your MySQL connections may use 127.0.0.1 or the IP address of your server, and MySQL will reject the connection if access isn't granted for the specific IP address used.
check the database name spelling at your phpMyAdmin. Usually the name is in format user_dbname.
For example:
cpanel username: jack,
database created: student
In your php script, the dbname should be jack_student
This worked for me:
Depending on what MySQL version you have, make sure you have matching password and hostname on your PHP file, config.inc.php file.
If you need to change the password for MySQL 5.7.6 and later:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
MySQL 5.7.5 and earlier:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
If you are trying to access your localhost server from a different machine i.e. (emulator, another computer), make sure you are using the actual IP address of the the localhost, DO NOT USE localhost as the hostname. Because this is like telling the machine to connect to itself - but the server is on a different IP address.
how to connect cpanel db from Localhost.
i know connect from localhost. But i need to connect from server.
i am trying like this
<?php
mysql_connect("208.91.199.141","username","password") or die(mysql_error());
mysql_select_db("db");
?>
i get an Error
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'glansade'#'49.206.54.187' (using password: YES) in C:\xampp\htdocs\calendar\db_server.php on line 2
Access denied for user 'glansade'#'49.206.54.187' (using password: YES).
How to solve this....? Please Help me. Thanks
Go to RemoteMysql from cpanel
you have to add %.%.
Then you will be able to connect from remote Db.
You will not want to add %.% to Remote Database Access Hosts as it is a security concern. The %.% is a wildcard and using that wildcard in access hosts like that will expose mysql to the internet and the possibility of unauthorized database access is greatly increased.
Based on your error, the IP you are connecting from is 49.206.54.187. You will want to use this IP address in Remote Database Access Hosts. If you do not know the IP address to use, you can check your IP at http://cpanel.net/myip
I also see that you are connecting with your cPanel account username. While this should work, it is the preference of many to setup a separate account for each Database. To accomplish this, follow these instructions.
In cPanel, select MySQL Databases
In the Databases section, under "MySQL Users", create a New MySQL user.
After creating that user, add that user to the Database in the section "Add User To Database"
Make sure to select all privileges.
Thanks!
I'm not an expert, but I think you should try :
mysql_connect("localhost","username","password") or die(mysql_error());
Instead of :
mysql_connect("208.91.199.141","username","password") or die(mysql_error());