dpi9530 2018-08-17 11:50
浏览 109
已采纳

使用不同的PHP版本从服务器B上的PHP mysqli访问服务器A上的MySql数据库

I'm trying to figure out, how is possible to retrieve records from MySql database tables, located on the server A, which is PHP Version 5.3.29 from server B php mysqli PHP Version 7.2.8

I found this example How to Connect to the Remote MySQL Database using PHP, but I don't understand, how it works in relation to what I'm trying to do, doubt if it is what I'm looking for

I'm new with php, and all that I can imagine, I should be to use connection data from server A for php mysqli on server B, and add access host IP of server B in Remote MySQL of server A, and seems like I have to add database user and password in MySQL databases on server A, but I'm not sure, where do I have to get it on server B

Server A:

Version by phpinfo();: PHP Version 5.3.29

Database server

Server: Localhost via UNIX socket
Server type: MySQL
Server version: 5.6.39 - MySQL Community Server (GPL)
Protocol version: 10
Server charset: UTF-8 Unicode (utf8)

Web server

cpsrvd 11.70.0.51
Database client version: libmysql - 5.1.73
PHP extension: mysqliDocumentation curlDocumentation mbstringDocumentation
PHP version: 5.6.30

phpMyAdmin

Version information: 4.7.7

Server B:

Version by phpinfo();: PHP Version 7.2.8

Database server

Server: Localhost via UNIX socket
Server type: MariaDB
Server version: 5.5.56-MariaDB - MariaDB Server
Protocol version: 10
Server charset: UTF-8 Unicode (utf8)

Web server

nginx/1.11.10
Database client version: libmysql - mysqlnd 5.0.11-dev - $Id: 
PHP extension: mysqliDocumentation curlDocumentation mbstringDocumentation
PHP version: 5.6.30

phpMyAdmin

Version information: 4.6.6

Any advice, guide or example would be very helpful

Edit:

My attempt by answer, first of all I've added IP of server B in Remote MySQL on server A. My conn.php, located on Server B to connect to Server A:

<?php
$servername = "Server_A_IP";
$username = "Server_A_database_user";
$password = "Server_A_database_user_password";
$db = "Server_A_database";

$conn = new mysqli($servername, $username, $password, $db);
$conn->set_charset('utf8');

if(!$conn)
{
    die ("Error on the Connection" . $conn->connect_error);
}
?>

and dbtab.php also located on Server B with conn.php in same folder:

<?php
 include 'conn.php';
$sql = "SELECT * FROM tab";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html>
    <h1 align="center">Database</h1>
    <table border="1" align="center" style="line-height:25px;">
    <tr>
    <th colspan="2">Records</th>
    </tr>
    <tr>
    <th bgcolor="#ff897f">id</td> 
    <th bgcolor="#ff897f">name</td>   
    </tr>
<?php
if($result->num_rows > 0)
{
   while($row = $result->fetch_assoc())
   {
    ?>
     <tr>
        <td bgcolor="#e5cac8"><?php echo $row['id']; ?></td>
        <td bgcolor="#e5cac8"><?php echo $row['name']; ?></td>
    <?php
   }
}
else
{
    ?>
    <tr>
    <th colspan="2">There's No data found!!!</th>
    </tr>
    <?php 
}
?>
</body>
</html>

but in result, when I'm tying to open www.site.com/folder/dbtab.php or www.site.com/folder/conn.php, page does not load, just freezes

ping of Server A:

enter image description here

  • 写回答

1条回答 默认 最新

  • doutongfu9484 2018-08-17 12:05
    关注

    You don't need php on Server A for this. You just need to configure your MySQL Server (Server A) to

    1. allow network access (via IP-addresses) and
    2. create a MySQL Account for the IP-address of Server B

    If you have set up this you can create a connection from php on Server B by using

    $mysqli = new mysqli("<IP of Server A>", "user", "password", "database");

    see the excellent php website on this: mysqli Connections

    The steps 1, 2 depend on your MySQL Server. If you have direct root access you need to edit the configuration files and allow networking, otherwise it depends on the hosting service.

    To create a user account with remote access issue the following SQL statement (or use a web GUI):

    GRANT <Rights> ON <db-name>.* TO 'username'@'<IP of Server B>';

    see the MySql manual on granting privileges to users: https://dev.mysql.com/doc/refman/5.5/en/grant.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器