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

2
  • 3
    Warning: You are using an obsolete database API which has been removed entirely from the latest version of PHP. You should use a modern replacement. Commented May 18, 2016 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 Commented May 18, 2016 at 18:10

2 Answers 2

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']);
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.