简体   繁体   English

PHP Mysql数据库连接被拒绝

[英]PHP Mysql database connection being refused

I am trying to display a simple table of one of the tables in my mySQL database, but am getting the below message when I load the page. 我正在尝试在mySQL数据库中显示其中一个表的简单表,但是在加载页面时却收到以下消息。

connection failed:Connection refused
Username    Password    PhysicianID

I have tested the credentials I use in my code over and over and am 100% sure that they are correct. 我已经一遍又一遍地测试了我在代码中使用的凭据,并且100%确信它们是正确的。 I am unsure as to what could be causing this issue. 我不确定什么可能导致此问题。 Below is my php code: 以下是我的php代码:

<!DOCTYPE html>
 <head>
<TITLE>Table That will NOT load</TITLE>
 </head>
<body>
<table>
    <tr>
        <th>Username</th>
        <th>Password</th>
        <th>PhysicianId</th>
    </tr>

<?php
    $conn = mysqli_connect('host', 'root', 'password', 'MySQLDatabase');
    if(!$conn) {
        die("connection failed:" . mysqli_connect_error());
    }
    $sql = "Select * from Users";
    $result = $conn-> query($sql);

    if($result -> num_rows > 0) {
        while ($row = $result-> fetch_assoc()) {
            echo "<td><td>". $row["username"] ."</td><td>". $row["username"] ."</td><td>". $row[
                "password"] ."</td><tr>";
            }
            echo "</table>";
        }
        else {
            echo "0 result";
        }

        $conn-> close();
    ?>
</table>
</body>
</html>

Please make sure that the mysql server is bound to the expected IP which is assigned to the host value you are using in mysql_connect . 请确保mysql服务器已绑定到预期的IP,该IP已分配给您在mysql_connect中使用的host值。

  • Find out IP: nslookup <host> 找出IP: nslookup <host>
  • On the Host, check bind address of mysql process: ps -eaf | grep mysql 在主机上,检查mysql进程的绑定地址: ps -eaf | grep mysql ps -eaf | grep mysql . ps -eaf | grep mysql (0.0.0.0/0 means bind on all IPs of the Host and is fine) (0.0.0.0/0表示在主机的所有IP上绑定,可以)

If you get different IPs for both commands, that is the culprit. 如果两个命令的IP不同,那就是罪魁祸首。 Esp. ESP。 if the second one only returns 127.0.0.1. 如果第二个仅返回127.0.0.1。 You need to adjust your mysql configuration to bind on the external IP address of the Host then. 然后,您需要调整mysql配置以绑定到主机的外部IP地址。

It should work fine but in this line: 它应该可以正常工作,但在此行中:

$conn = mysqli_connect('host', 'root', 'password', 'MySQLDatabase');

Check if your host name and password is correct. 检查您的主机名和密码是否正确。 If you use localhost and your password is not defined it should be: 如果您使用localhost并且未定义密码,则密码应为:

$conn = mysqli_connect('localhost', 'root', '', 'MySQLDatabase');

Also, check your HTML in php. 另外,在php中检查HTML。 You have two <td> tags on beggining and using this order of $row you will get password under PhysicianId in table. 您在开始时有两个<td>标记,使用$row此顺序,您将在表中的PhysicianId下获得密码。

Try this: 尝试这个:

if($result -> num_rows > 0) {
    while ($row = $result-> fetch_assoc()) {
        echo "<tr><td>". $row["username"] ."</td><td>". $row["password"] ."</td><td>". $row[
            "id"] ."</td></tr>";
        }
        echo "</table>";
    }
    else {
        echo "0 result";
    }

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

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