connection to mysql either remote or local with same code

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

Well, certainly you can set a parameter to be passed in the URL

http://domain.com/index.php?host=localhost

Then in the code you could do something like:

if (!empty($_GET['host'])) {
    $miconexion->conectar("jorlal8_nueva", $_GET['host'], "jorlal8_nueva", "******");//local
} else {
   $miconexion->conectar("jorlal8_nueva", "mysql4.freehostia.com", "jorlal8_nueva", "******");//remote
}

Security Note: you should ALWAYS validate variables to ensure they have the intended data prior to using. I did not in this example.

In this example the “Default” Host would be mysql4.freehostia.com, but you could simply alter it to ANY host by adding the host parameter to the URL.

You could also use the $_SERVER[‘SERVER_NAME’] variable to make the determination

// Use whatever the "Appropriate" Server name would be
if ($_SERVER['SERVER_NAME'] == "freehosit.com") {
   $miconexion->conectar("jorlal8_nueva", "mysql4.freehostia.com", "jorlal8_nueva", "******");//remote
} else {
    $miconexion->conectar("jorlal8_nueva", "localhost", "jorlal8_nueva", "******");//local
}

Thank you very much for your replies, I am learning a lot from you guys and those two replies were really good ones.

Sponsor our Newsletter | Privacy Policy | Terms of Service