Page 1 of 1

PHP database connection problem in webserver

Posted: Fri Aug 19, 2011 5:30 am
by tanvirtonu
Hello I m new to php. I have uploaded a simple php script in my web root - http://www.mydomain.com which will connect to my DB.
But strangely I CANT connect to my DB if I put the hostname as my real hostname - mydomain.com . But if I use - localhost , it works. How come its working as- localhost when I am not on localhost.

Code: Select all

<?php
echo "HELLO WORLD";
$connect = mysql_connect("localhost","username","mypassword") or die ("couldn't connect db"); // it works
//$connect = mysql_connect("www.mydomain.com","username","mypassword") or die ("couldn't connect db"); // it DOESN'T work
?>

Re: PHP database connection problem in webserver

Posted: Fri Aug 19, 2011 5:34 am
by genix2011
Hi,

if your mysql server is on the same server as the web server (Apache, ..), the hostname for the mysql server is localhost.

Greets.

Re: PHP database connection problem in webserver

Posted: Fri Aug 19, 2011 5:59 am
by tanvirtonu
DO u mean if there are MySQL server and Apache server , the hostname is - localhost .
What do u mean by ON THE SAME SERVER ? Arn't they always on the same server ?
And how do I know if they are on the same server .

Re: PHP database connection problem in webserver

Posted: Fri Aug 19, 2011 8:20 am
by genix2011
They are not always on the same server, the mysql server can be on another server, then the hostname would be different than localhost. If you have a normal webspace the mysql server has the hostname as described by the webspace provider.
If you have installed a Server yourself, only then you know if it is on the same server. (Your Desktop computer)

Re: PHP database connection problem in webserver

Posted: Sat Aug 20, 2011 8:12 am
by tanvirtonu
thanx a lot bro..