PHP database connection problem in webserver

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tanvirtonu
Forum Commoner
Posts: 35
Joined: Wed Oct 17, 2007 9:15 am

PHP database connection problem in webserver

Post 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
?>
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: PHP database connection problem in webserver

Post 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.
tanvirtonu
Forum Commoner
Posts: 35
Joined: Wed Oct 17, 2007 9:15 am

Re: PHP database connection problem in webserver

Post 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 .
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: PHP database connection problem in webserver

Post 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)
tanvirtonu
Forum Commoner
Posts: 35
Joined: Wed Oct 17, 2007 9:15 am

Re: PHP database connection problem in webserver

Post by tanvirtonu »

thanx a lot bro..
Post Reply