如何设置mysql数据库连接名

时间:2022-04-03 12:10:06

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.

我想知道如何在同一个页面上放置两个不同的MYSQL数据库连接名称,一个用于本地PC,一个用于托管服务器。

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

alidad

2 个解决方案

#1


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']);

#2


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.

方法是设置一个配置。本地主机中的ini文件,以及生产环境中的另一个。这样,您将解析该文件并获得您的凭证。

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

更好的方法是使用这个库:https://github.com/vlucas/phpdotenv

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

它基本上以相同的方式工作,但是很容易维护。

#1


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']);

#2


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.

方法是设置一个配置。本地主机中的ini文件,以及生产环境中的另一个。这样,您将解析该文件并获得您的凭证。

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

更好的方法是使用这个库:https://github.com/vlucas/phpdotenv

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

它基本上以相同的方式工作,但是很容易维护。