connecting to mysql database(either remote or local) from ph

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
thosecars82
Forum Commoner
Posts: 94
Joined: Thu Apr 03, 2008 6:31 am
Location: Arganda, Madrid
Contact:

connecting to mysql database(either remote or local) from ph

Post by thosecars82 »

Hello people
does anyone have any idea to test a php application using database first in local and afterwards in the remote server?
What i mean is, does anyone know how to do it without having to change the code used to get the connection to the database.
My code to connect to the database looks like this:
//$miconexion->conectar("jorlal8_nueva", "mysql4.freehostia.com", "jorlal8_nueva", "******");//remote
$miconexion->conectar("jorlal8_nueva", "localhost", "jorlal8_nueva", "******");//local
Each time i want to test on remote I have to comment the line to connect to mysql in localhost so that I can be testing at the remote mysql database which in my case is in freehostia. But if i want to test later at localhost i'll have to change the code again.
Does anybody knows how to do this?
Someone told me that he used application variables to know whether the code was running local or not. Besides he told me another posibility which was using xml tags to distinguish between debugging and normal run of the application. Nevertheless, I am still very lost.
Please I would appreciate any help about this.
Thank you
HaddyConsulting
Forum Newbie
Posts: 2
Joined: Fri Apr 04, 2008 9:32 am

Re: connecting to mysql database(either remote or local) from ph

Post by HaddyConsulting »

Your question is lacking a few details, so I'll try to point you in the right direction...

First, when you are testing the remote database, are you also testing with remote based scripting? Say is your scripting being ran locally, or from your server? In theory, if you code via localhost on both connections, upload your code to the server, it will still be localhost.

Are you using wamp or lamp locally?

If you run the code locally, you will connect to your wamp/lamp mysql, but if you upload it, it will connect to the localhost database on your server.

Second, there's hundreds of ways you can make it dynamically connect to a local or remote based database just on simple coding syntax... Here's one option... set a GET variable and change your url depending on what database you want to test...

if($_GET['con'] == "server"){
$miconexion->conectar("jorlal8_nueva", "mysql4.freehostia.com", "jorlal8_nueva", "******");//remote
}
else{
$miconexion->conectar("jorlal8_nueva", "localhost", "jorlal8_nueva", "******");//local
}

Now all you have to do here is, when you want to test on the server, you enter yourfile.php?con=server, otherwise you just run yourfile.php normally, the con=server provides the switch statement to go to the server.
thosecars82
Forum Commoner
Posts: 94
Joined: Thu Apr 03, 2008 6:31 am
Location: Arganda, Madrid
Contact:

Re: connecting to mysql database(either remote or local) from ph

Post by thosecars82 »

thks for ur reply, it's interesting. Nevertheless, for the time being, I eventually used this, which actually works pretty easily.
if ($_SERVER['HTTP_HOST']!="localhost")
$miconexion->conectar("jorlal8_nueva", "mysql4.freehostia.com", "jorlal8_nueva", "****");
else
$miconexion->conectar("jorlal8_nueva", "localhost", "jorlal8_nueva", "****");
Post Reply