First, let me start by saying the website was working perfectly well on Dreamhost (except for the last week with horrible lags and downtime - but it was not what made me move away from them, read below).
I was hosting my website on Dreamhost. I had to move due to a problem with the owner of the revenue hosting (he'd not let me change a dns config on the panel nor would do it for me).
So I signed with HostGator, and now I'm stuck with this MySQL issue. Here's what I've done so far:
First, I uploaded all the files via ftp to the HostGator server. Then, I exported the database from the old server and imported it onto the new one. Then I changed the name server on the domain registrar, and finally, after a while, hit F5 on the browser.
And then there was a huge ammount of error lines. So I searched the HostGator Support Portal and found out that I had to change from this (the code I was using before):
<?
date_default_timezone_set('America/Sao_paulo');
if ($_SERVER['HTTP_HOST']=='localhost') {
$conexao = mysql_connect("localhost");
$bd = mysql_select_db("tablename");
} else {
$conexao = mysql_connect("mysite.phpmyadmin.com","mylogin","mypassword");
$bd = mysql_select_db("mydatabasename");
}
mysql_query("SET time_zone='-3:00'");
?>
To something like this:
<?
define('DB_NAME', 'mydatabasename');
define('DB_USER', 'mylogin');
define('DB_PASSWORD', 'mypassword');
define('DB_HOST', 'localhost');
?>
The thing is I'm no PHP or MySQL expert. The programmer let me without support, so I'm on my own.
When I try to load my site, it gives the following errors:
Warning: mysql_query() [function.mysql-query]: Access denied for user 'mylogin'#'localhost' (using password: NO) in /home/mylogin/public_html/index.php on line 42
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/mylogin/public_html/index.php on line 42
ERROR 40 >Access denied for user 'mylogin'#'localhost' (using password: NO)
On the file index.php, line 42 is this:
$sqlo = mysql_query("select *, max-buyers as left from offers inner join rel_city on rel_city.id_offer = offers.id where id_city=$city and startdate <= now() and enddate >= now() order by priority desc, left desc, enddate asc") or die("ERROR 40 >".mysql_error());
One important detail is that I need these two lines to work with the new hosting, but whenever I try to insert them, it gives new errors:
date_default_timezone_set('America/Sao_paulo');
mysql_query("SET time_zone='-3:00'");
Any ideas of how I can get this working?
Try this:
<?
date_default_timezone_set('America/Sao_paulo');
$conexao = mysql_connect("localhost","mylogin","mypassword");
$bd = mysql_select_db("mydatabasename");
mysql_query("SET time_zone='-3:00'");
?>
What I've done is simplify the original code to remove the IF / ELSE statement which looks like it might be there for development purposes.
If you're getting other errors not posted, please include them.
I'm not familiar with HostGator's setup, but what seems to be the issue is either the address or credentials you are trying to access the server with.
Perhaps HostGator prepends a prefix to usernames and database names? (ex. maybe it should be something_mydatabasename rather than simply mydatabasename)
Or perhaps their setup doesn't support using localhost as the database address; perhaps they have a defined and isolated address for you to connect to.
I'd give support a shout, or double-check the addresses, usernames, and database names in your cPanel settings. It may give you a good start.
(This advice is, of course, assuming that you have the username and password for the database correct, which the error suggests is not the case.)
From the errors, it looks like you don't have the DB user and password setup in MySQL.
Your database has a username and password. Make sure that you have specified them correctly, or you won't be able to connect to your database.
The error messages you're seeing are due to your programmer using the PHP die() function, which isn't really good practice.
Check with your host and make sure that you have permissions on your tables, as well. You should, but you never know.
Related
An application was handed to me by an old colleague to manage. However, whenever I try to run it, I keep getting this error. Please any help would do.
Sometimes Access denied Exception Error because your mysql credentials are invalid.
Secondly, from my experience i observed that this also happens because you did not set password to your database connectivity. eg
private $host = "localhost";
private $db_name = "db_dbtest"; // Database name
private $username = "db_user"; // your database username
private $password = "db_password"; // Your password
public $conn;
}
Try and set password to your database connectivity. I had such experience and after changing my Collation to utf8_general_ci on the Operations tab, this could not solve my problem. I thought of adding password to my database connection and immediately it connected.
You can tesrun this and see if it helps.
You're getting Access denied Exception Error because your mysql credentials are invalid.
This is due to the wrong sql information provided by you.Changing it to the right one can solve this error .
I was getting this same error code. I'm using phpMyAdmin and added the database with the import of a sql file. The website was using all the same code that had worked before. To fix this error go inside phpMyAdmin and click on the database then operations tab. Change the collation of my database to "ut8_general_ci". Afterwards it was able to verify the username/password access to the database.
Also you might want to make sure you added the user accounts tab the username,password and privileges to access the database.
I know this is an old question, but for me I had to set my password in Sequel Pro to the password that was listed in my .env config file. Hope this helps someone!
Well it means the SQL credentials you are using are wrong there is nothing much we can do, you need to use the correct ones or use the root account to change the user's password or rights.
You have to specify the port you are using for your MySQL. for me, I was changed the port of MySQL like 3307.
example:localhost:3307
The first thing you should check is your credentials. I thought i had root as username and password but when i looked in the file C\xampp\phpMyAdmin\config.inc.php under
$cfg['Servers'][$i]['password'] = 'WhateverPassword';
I saw that i had a different default password. You should use or change the password that is in that file when trying to connect to your database.
Make sure you have the right credentials and these are being passed to the database connection command.
Also make sure to either wrap the credentials in single quotes ' or escape them with backslash \, as there are some special characters not recognised. Hope this helps someone. Here's a reference:
String Literals
The site you got handed has the login credentials for your colleague's machine/site. Depending on the site, different files need to be changed. Example, Wordpress usually has the settings in a file called wp-config.php
Until you find this file, maybe you can change your XAMPP user login to match. This article on SO tells how to change SQL login credentials:
How do I use MySQL through XAMPP?
After update my Ubuntu to 13.10 in php , mysql can connect by any user but can't select database with correct user and pass.
seem the User that sent to mysql is empty Instead root.
$myconn = mysql_connect('localhost' , 'root' , 'pass');
$seldb = mysql_select_db('mydb' , $myconn);
$er = mysql_error();
After run
$mycon is:"resource id='2' type='mysql link'"
and
mysql_select_db() return false;
and
$er is:"Access denied for user ''#'localhost' to database 'mydb'"
Please help me.
Sorry for my bad English.
You see MySQL thinks you're '', not root, which is related to this warning from the manual:
If you permit anonymous users to connect to the MySQL server, you should also grant privileges to all local users as user_name#localhost. Otherwise, the anonymous user account for localhost in the mysql.user table (created during MySQL installation) is used when named users try to log in to the MySQL server from the local machine. For details, see Section 6.2.4, “Access Cotrol, Stage 1: Connection Verification”.
“Access Cotrol, Stage 1: Connection Verification” states:
The server uses sorting rules that order rows with the most-specific Host values first
That means if you have an anonymous ''#localhost user, but a less specific (or just sorted later) root#'%' (or something like it) definition never matches because the 'anonymous' rule matches. Look through that last link for a more thorough explanation.
What this means is you either:
GRANT access to root#localhost explicitly.
DROP USER ''#'localhost'; (the anonymous user)
I would highly recommend the second one: you should have no need for an anonymous user.
I Solve this problem myself. in php.ini file sql.safe _mode was on.
that cause mysql_connect() Always connect as anonymous Ubuntu User.
in php.ini file:
sql.safe_mode = Off
I usually connect php to mysql with localhost in my PC..
now i'm trying to put my project in cloud https://c9.io ,but i can't connect to mysql. i already have mysql database in cloud and put my project in same place...
mysql_connect("/lib/mysql/socket/mysql.sock","myUser","") or die(mysql_error());
i use script above to connect but i get Unknown MySQL server host '/lib/mysql/socket/mysql.sock' (1)
what shoul i do ?
Okay, so none of the above answers had worked for me, but fortunately I was able to setup a database and get it up and running my own way and I can now make queries and run them successfully, so I will share my method with you in hopes that anyone else scouring the internet can stumble across this and not have to go through the same head scratching that I did.
If you want the quick rundown, just scroll to Step 3 and read on from there. If you're a complete beginner, keep reading as I'll walk you through it in detail.
Couple things to mention:
You will have to setup a database via a Terminal in Cloud 9. I had no experience prior doing it in a Terminal before, but it's very simple to learn.
You can not use mysql functions, you have to use mysqli, since mysql functions are deprecated and Cloud 9 will not run them.
Step 1: Setup MySQL on Cloud 9 (in Terminal)
In your project, open up a New Terminal (click the plus-sign tab above the text editor space, select "New Terminal"). In the terminal, type mysql-ctl start and hit Enter. MySQL will start up in the back, but you won't get any response back in the terminal.
Next, type mysql-ctl cli and hit Enter. You should see some text that starts off as Welcome to the MySQL monitor.... Congrats, you've setup MySQL on your Cloud 9 project.
Step 2: Create a test database (in Terminal)
You can actually go ahead and create your official database if you like, but for this sake I'll just make a database that holds a table that holds an ID and a username. So here's the steps to setting up a database and a table. If you've used MySQL and databases before, then this should be cake, but I'll explain it in detail for those who might not fully understand MySQL .
Type SHOW DATABASES; and hit Enter. This will show a list of current databases within your project. You can enter this any time you want to see a list of your databases on the current project.
Type in CREATE DATABASE sample_db; and hit Enter. You should get a Query OK, 1 Row affected. which means the query was successful. You can name the database whatever you like, but for this little walk-through, I named it sample_db.
Type in USE sample_db; and hit Enter. This selects sample_db from the list of databases.
Type in CREATE TABLE users (id INT(11), username VARCHAR(20));, and hit Enter. This creates a table named users with two columns: id and username. The number in parentheses represents the character limit the column will store in the database. In this case for example, username won't hold a string longer than 20 characters in length.
Type in INSERT INTO users (id, username) VALUES (1, "graham12");, and hit Enter. This will add the id of 1 and a username graham12 in the table. Since the id column is an INT, we do not put quotes around it.
Type in SELECT * FROM users;, and hit Enter. This will show everything that is in the users table. The only entry in there should be what we inserted from the last step we just did.
Step 3: Get the credentials you'll need to connect to the database from PHP. (in Terminal)
Now we have some data in our table that we can test our mysqli connection with. But first, we have to get the credentials we will need to connect to the database in PHP. In Cloud 9, we will need 5 credentials to connect:
Host name
Username
Password
Database name
Port #
Username, password, database name, and port #, are practically already known to you by now. I'll explain:
Host name - Type in SHOW VARIABLES WHERE Variable_name = 'hostname';, and hit Enter. You'll get a table that has 2 columns: Variable_name and Value. In the Value column you should see something like yourUsername-yourProjectName-XXXXXXX, where the X's are a 7 digit number. Write this number down or save it some where. This is your host name. (If you're getting the quick rundown on this walkthrough, just start a new terminal and start up your mysql and select the database you want to use, then type in SHOW VARIABLES WHERE Variable_name = 'hostname';. Re-read this step from the beginning if you're confused.)
Username - Your username that you use to log in to Cloud 9.
Password - There is NO password for your database in Cloud 9.
Database name - This would be sample_db or whatever you named your database;
Port # - is 3306. In Cloud 9, all of your projects are wired to 3306. This is a universal constant of Cloud 9. It will not be anything else. Write this as you would an integer, not as a string. mysqli_connect() will interpret the port # as a long data type.
Last Step: Connect to the database with PHP! (using PHP)
Open up a PHP file and name it whatever you like.
I'll pretend that my host name is graham12-sample_db-1234567 for this example and that this is what my data looks like:
Host name: "graham12-sample_db-1234567"
Username: "graham12"
Password: ""
Database name: "sample_db"
Port #: 3306
So in PHP, insert your credentials accordingly:
<?php
//Connect to the database
$host = "grahamsutt12-sample_db-1234567"; //See Step 3 about how to get host name
$user = "grahamsutt12"; //Your Cloud 9 username
$pass = ""; //Remember, there is NO password!
$db = "sample_db"; //Your database name you want to connect to
$port = 3306; //The port #. It is always 3306
$connection = mysqli_connect($host, $user, $pass, $db, $port)or die(mysql_error());
//And now to perform a simple query to make sure it's working
$query = "SELECT * FROM users";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
echo "The ID is: " . $row['id'] . " and the Username is: " . $row['username'];
}
?>
If you get a result and no error then you have successfully setup a database and formed a connection to it with PHP in Cloud 9. You should now be able to make all the queries you can normally make.
Note: I demonstrated the last part without using parameterized queries for the sake of being simple. You should always use parameterized queries when working with real web applications. You can get more info on that here: MySQLi Prepared Statements.
For starters, the mysql_* functions are deprecated so you shouldn't be using them. Look at PDO or mysqli instead. Next, you'll want to try this per the example docs:
$link = mysql_connect('localhost:/lib/mysql/socket/mysql.sock', 'myUser', '') or die(mysql_error());
To find the ip running you project, create a test file with the code below, run it and put the result as host.
<?php
$ip = getenv("REMOTE_ADDR") ;
Echo "Your IP is " . $ip;
?>
You are using Cloud9 so it's a little different to use. To connect to MySQL you have to first create the MySQL server in C9. Type this in C9's command line:
mysql-ctl start
C9 will create your mysql server.
MySQL 5.1 database added. Please make note of these credentials:
Root User: <username>
Database Name: c9
Next to find your IP address type:
echo $IP
Now use this code with your username, the ip address, no password and the 'c9' database to access MySQL:
mysql_connect("<$IP>","<username>","") or die(mysql_error());
mysql_select_db("c9")
Hope this helps
The documentation show how start, stop, and run the mysql environment.
Start the MySQL shell mysql-ctl start then in yor file.php:
$ip = getenv("REMOTE_ADDR");
$port = "3306";
$user = "YorUsername";
$DB = "c9";
$conn = mysql_connect('$ip', '$user', '', '$db', '$port')or die(mysql_error());
mysql_select_db('$db','$conn')or die(mysql_error());
mysql_query("select * from YourTableName",'$conn')or die(mysql_error());
The line getenv("REMOTE_ADDR") return the same local IP as the application you run on Cloud9.
This is the code that connects to my SQL database. I'm new with this stuff and it seems to be semi-working but certain features on my website still don't work.
<?php
$con = mysql_connect("localhost","username","password");
$select_db = mysql_select_db('database1',$con);
/*$con = mysql_connect("localhost","username2","password2");
$select_db = mysql_select_db('database2',$con);*/
?>
This is the site in question: http://tmatube.com keep in mind the credentials above are filled in with what the programmer used for testing on his own server... ;) unfortunately I don't have access to him for support anymore.
Anyway, here's my thoughts on how this code needs to be edited maybe someone can chime in and let me know if I'm correct in my assumptions:
<?php
$con = mysql_connect("localhost","username1","password1"); -------------<<< leave this line
$select_db = mysql_select_db('DATABASE_NAME_HERE',$con);
/*$con = mysql_connect("localhost","DB_USERNAME_HERE","DB_PASSWORD_HERE");
$select_db = mysql_select_db('DATABASE_NAME_HERE',$con);*/
?>
Ok - now on to a few problems I noticed...
What does this do? /* code here */? It doesn't work at all if I leave that bit in.
Why is it connecting to database twice? and is it two separate databases?
$select_db = mysql_select_db('DATABASE_NAME_HERE',$con); <<<---- single '
When I tried to see if that line was correct the examples I saw had quotes like this
$select_db = mysql_select_db("DATABASE_NAME_HERE",$con); <<<---- double "
Which one is right?
He didn't leave it out. What he did was leave the database to be connected using the root, which has no password. The other connection (which is commented out) is using another user, rajvivya_video, with a password defined.
In testing it MIGHT be okay to connect to root and leave it without password, but even that is not recommended, since its so easy to work with a user and password defined (besides root).
Here is php mysql connect with mysqli:
<?php
$link = mysqli_connect("myhost","myuser","mypassw","mybd");
?>
No difference here with ' or ". (Anyway use mysqli and you can the wanted db as 4th parameter.) php quotes
/* comment */ is a commented out so the php does not care what is inside so only 2 first rows of are affecting (they are same mysql database on the local machine and 2 different user + password combinations). Comment in general are used to explain the code or removing part of the code with out erasing it. php commenting
Is it possible to connect to a database lying on different server through another server ?
For example :-
I want to access the users table of www.abc.com from www.xqz.com, both are on different server.
Can you give me any ideas ?
will really appreciate if provide me the php query to connect.
Thanks.
"Is it possible" is a bit over-reaching, I think. To give you the shortest answer, yes, it is possible. However, whether or not you could do it depends on a lot of variables, not least of which is whether your hosting providers allow it. For instance, I use Hostmonster for Linux hosting, and to remotely access my MySQL database I have to first whitelist the IP address of the machine that will be accessing it.
Most hosting services provide a F.A.Q. section that should do a decent job of answering this particular question for you. Some will even provide support technicians help you set it up (Hostmonster does, within reason).
HTH.
Try This :
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
// Suppose your "www.abc.com" has an IP = 192.168.1.1
// So to connect, do something like this :
$server = '192.168.1.1,PORT_NUMBER'; // FOR WINDOWS
$server = '192.168.1.1:PORT_NUMBER'; // FOR LINUX
// Connect to MSSQL
$link = mssql_connect($server, [username], [password]);
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
?>