简体   繁体   English

如何设置mysql数据库连接名

[英]how to set mysql database connection name

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 我在本地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

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

The way is to have a config.ini file in your localhost, and a different one in your production environment. 方法是在本地主机中有一个config.ini文件,在生产环境中有一个不同的文件。 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 更好的方法是使用此库: https : //github.com/vlucas/phpdotenv

It works basically on the same way, but is easy to maintain. 它基本上以相同的方式工作,但易于维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM