【问题标题】:Cannot access multiple columns from multiple databases outside for loops无法从 for 循环之外的多个数据库中访问多个列
【发布时间】:2018-10-25 17:13:37
【问题描述】:

给了我一个项目,拿了很多php5代码,更新到php7(主要是把所有mysql_函数改成mysqli_)。特别是对于这个文件,我需要显示来自 3 个不同数据库的不同信息列。如果这只是一个查询,那么我可以将我的 html 放在一个循环中,但我正在跨多个数据库获取信息。

在检查我的 apache error.log 时,我不断收到 html 中每个变量的“未定义变量”。

如何正确获取这 3 个查询的结果,以便稍后在页面上使用?

注意:我的配置文件是正确的,因为所有其他页面都可以访问这些数据库。

$Master_Status_Query = "show master status";
$NS10_Status_Query = "show slave status";
$NS11_Status_Query = "show slave status";

$master_result = $master_conn->query($master_conn, $Master_Status_Query);
$ns10_result = $ns10_conn->query($ns10_conn, $NS10_Status_Query);
$ns11_result = $ns11_conn->query($ns11_conn, $NS11_Status_Query);


if($master_result === FALSE) {
    print '<p class="input--error">Something broke: ' . mysqli_error($master_conn);
} else {
    foreach($master_result as $master_row) {
        $Master_Log_Name = $master_row['File'];
                $Master_Log_Position = $master_row['Position'];
    }
}
if($ns10_result === FALSE) {
    print '<p class="input--error">Something broke: '. mysqli_error($ns10_conn);
} else {
    foreach($ns10_result as $ns10_row) {
        $NS10_Log_Name = $ns10_row['Master_Log_File'];
        $NS10_Log_Position = $ns10_row['Read_Master_Log_Position'];
        $NS10_Seconds_Behind_Master = $ns10_row['Seconds_Behind_Master'];
    }
}
if($ns11_result === FALSE) {
    print '<p class="input--error">Something broke: '.mysqli_error($ns11_conn);
} else {
    foreach($ns11_result as $ns11_row) {
        $NS11_Log_Name = $ns11_row['Master_Log_File'];
        $NS11_Log_Position = $ns11_row['Read_Master_Log_Position'];
        $NS11_Seconds_Behind_Master = $ns11_row['Seconds_Behind_Master'];
    }
}

mysqli_close($master_conn);
mysqli_close($ns10_conn);
mysqli_close($ns11_conn);


echo "<table id=\"replication_table\">";
echo "<tr><td colspan=\"3\">&nbsp;</td></tr>";
echo "<tr><td colspan=\"3\" align=\"center\">Server Replication Status</td></tr>";
echo "<tr><td colspan=\"3\">&nbsp;</td></tr>";
echo "<tr><td colspan=\"3\">&nbsp;</td></tr>";
echo "<tr bgcolor=\"darkblue\"><td width=\"30%\"align=\"left\"><font color=\"white\">Server</font></td><td width=\"30%\"align=\"left\"><font color=\"white\">Log File</font></td><td colspan=\"3\" width=\"30%\"align=\"left\"><font color=\"white\">Log Position</font></td></tr>";
echo "<tr><td align=\"left\">Master</td><td align=\"left\">$Master_Log_Name</td><td align=\"left\">$Master_Log_Position</td><td align=\"left\">&nbsp;                     </td><td align=\"left\">&nbsp; </td></tr>";
echo "<tr><td align=\"left\">NS10  </td><td align=\"left\">$NS10_Log_Name  </td><td align=\"left\">$NS10_Log_Position  </td><td align=\"left\">$NS10_Seconds_Behind_Master</td><td align=\"left\">&nbsp; </td></tr>";
echo "<tr><td align=\"left\">NS11  </td><td align=\"left\">$NS11_Log_Name  </td><td align=\"left\">$NS11_Log_Position  </td><td align=\"left\">$NS11_Seconds_Behind_Master</td><td align=\"left\">&nbsp; </td></tr>";
echo "<tr><td align=\"left\"><button class='btn btn-default' type=\"button\" onClick=\"myRef = location.replace('db-integrity-check.php','mywin');\">Back To Menu</button></td></tr></br>";
echo "</table>";

【问题讨论】:

    标签: php mysqli php-7


    【解决方案1】:

    把代码改成这样后:

    $Master_Status_Query = "show master status";
    $NS10_Status_Query = "show slave status";
    $NS11_Status_Query = "show slave status";
    
    $master_result = mysqli_query($master_conn, "show master status");
    $ns10_result = mysqli_query($ns10_conn, "show slave status");
    $ns11_result = mysqli_query($ns11_conn, "show slave status");
    
    $master_row = $master_result->fetch_array();
    $ns10_row = $ns10_result->fetch_array();
    $ns11_row = $ns11_result->fetch_array();
    
    $Master_Log_Name = $master_row['File'];
    $Master_Log_Position = $master_row['Position'];
    
    $NS10_Log_Name = $ns10_row['Master_Log_File'];
    $NS10_Log_Position = $ns10_row['Read_Master_Log_Pos'];
    $NS10_Seconds_Behind_Master = $ns10_row['Seconds_Behind_Master'];
    
    $NS11_Log_Name = $ns11_row['Master_Log_File'];
    $NS11_Log_Position = $ns11_row['Read_Master_Log_Pos'];
    $NS11_Seconds_Behind_Master = $ns11_row['Seconds_Behind_Master'];
    
    
    
    mysqli_close($master_conn);
    mysqli_close($ns10_conn);
    mysqli_close($ns11_conn);
    

    我收到拒绝访问错误。授予数据库 SUPER 权限后,记录正确显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-04
      • 2015-01-27
      • 2021-06-29
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      相关资源
      最近更新 更多