【问题标题】:How to connect multi remote sql server?如何连接多个远程sql server?
【发布时间】:2021-02-25 08:46:42
【问题描述】:

我的本​​地 sql 太大了。

现在我想添加另一个远程mysql。

任何人都可以在以下本地mysql连接中设置远程mysql吗?

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"

#Localhost mysql: Make it only read

$hostname_saha = "localhost";
$database_saha = "bdwebstk_1a";
$username_saha = "bdwebstk_1a";
$password_saha = "111111111111"; 

#remote mysql : Plesae make it read and write

$hostname_saha = "xx.yy.zz.zz";
$database_saha = "horjecom_aw";
$username_saha = "horjecom_aw";
$password_saha = "000000000000000000"; 

$saha = mysqli_connect($hostname_saha, $username_saha, $password_saha, $database_saha) or trigger_error(mysqli_error($saha),E_USER_ERROR); 

mysqli_set_charset( $saha, 'utf8');

?>

【问题讨论】:

  • 您遇到错误了吗?
  • 上面只放了我的代码。它没有被编辑。你可以编辑它,就好像只有 localhost 会读取而 Remote sql 会读取和写入。

标签: php mysql mysqli


【解决方案1】:

您必须为每个 SQL Server 创建单独的连接 - 最好在单独的字段中。

我强烈建议从他们将服务的业务中命名您的连接名称。 另一个强烈建议您将配置文件存储在代码库之外https://stackoverflow.com/a/3724607/3711660

文件 localhost_sql.php

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"

#Localhost mysql: Make it only read

$hostname_saha = "localhost";
$database_saha = "bdwebstk_1a";
$username_saha = "bdwebstk_1a";
$password_saha = "111111111111"; 

$sqlcon_localhost= mysqli_connect($hostname_saha, $username_saha, $password_saha, $database_saha) or trigger_error(mysqli_error($saha),E_USER_ERROR); 

mysqli_set_charset( $sqlcon_localhost, 'utf8');
?>

文件remote_sql.php

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"

#Localhost mysql: Make it only read


$hostname_saha = "66.96.566.53";
$database_saha = "horjecom_aw";
$username_saha = "horjecom_aw";
$password_saha = "000000000000000000"; 


$sqlcon_remote= mysqli_connect($hostname_saha, $username_saha, $password_saha, $database_saha) or trigger_error(mysqli_error($saha),E_USER_ERROR); 

mysqli_set_charset( $sqlcon_remote, 'utf8');
?>

然后在下面的另一个文件中使用它

require('remote_sql.php');
$result = mysqli_query($sqlcon_remote, "SELECT DATABASE()")

【讨论】:

  • 我将把这段代码放在哪里?要求('remote_sql.php'); $result = mysqli_query($sqlcon_remote, "SELECT DATABASE()") 这里是结果的完整代码www20.zippyshare.com/v/dANfYkU7/file.html请告诉我
  • 您已经在第一行中包含了连接。 &lt;?php require_once('Connections/saha.php'); ?&gt; - 很好,你可以把它保存在已经存在的文件中,但是用变量名分割声明或者创建一个单独的 Connections/saha_remove.php 文件,然后在你的主文件中需要它:)
  • 我按照以下方式输入数字但得到错误 查看我的代码www20.zippyshare.com/v/dANfYkU7/file.html
  • 目前还没有错误。我想在 localhost php 脚本中连接多个远程 sql。我的 localhost sql 大小太大。现在我希望 localhost 只会从 sql 读取数据。但是远程mysql会读写。因此,我想在我的站点中添加多个远程 sql。在上面我给出了我的 localhost mysql 详细信息和远程。
  • 我的代码解决了你的问题:P 问题是你只有一个连接,但你需要两个独立的连接。您是在谈论主/从配置还是什么? digitalocean.com/community/tutorials/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-17
  • 2021-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-19
相关资源
最近更新 更多