1

I'm trying to figure out how can I put two differents MYSQL database connections name on the same page, one is for local PC and one is for hosting server.

Does is possible to have two different databases servers name on the same page so that way not to change connection database name in a script before upload.

I have code like this for local PC

<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>

Can can i add another connection name in the server in same page without changing the connection database!

alidad

alidads
  • 35
  • 8
  • 3
    **Warning**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) which has been **removed** entirely from the latest version of PHP. You should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin May 18 '16 at 18:09
  • What exactly are you trying to do? To connect to 2 different databases on the same server? Your wording is kind of confusing – Miguel Guerreiro May 18 '16 at 18:10

2 Answers2

2

Yes, a single page may query as many different servers as needed:

<?php
$server1 = new mysqli("server1.example.com", "user1", "password1", "database1");    
$server2 = new mysqli("server2.example.com", "user2", "password2", "database2");

$result = $server1->query("SELECT 'Hello user' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);

$result = $server2->query("SELECT 'Hello user' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);
wallyk
  • 56,922
  • 16
  • 83
  • 148
0

The way is to have a config.ini file in your localhost, and a different one in your production environment. This way you will parse this file and get your credentials.

A better way to do that is using this library: https://github.com/vlucas/phpdotenv

It works basically on the same way, but is easy to maintain.

Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29