MySQL issue when moving from Dreamhost to HostGator
First, let me start by saying the website was working perfectly well on Dreamhost (开发者_StackOverflow中文版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.
精彩评论