在php / mysqli中打印表的问题

时间:2017-04-29 22:42:06

标签: php mysqli

所以基本上我的代码不在这里工作。令人困惑的部分是,至少它应该打印表头,它没有这样做。

这是我的功能

public function getUsers(){
    global $connectstr_dbhost, $connectstr_dbname, $connectstr_dbpassword, $connectstr_dbusername;
    $link=mysqli_connect($connectstr_dbhost, $connectstr_dbusername, $connectstr_dbpassword,$connectstr_dbname);
    $sql = "SELECT * FROM `users`";
    $result = mysqli_query($link, $sql);



    echo ("
         <table border='1'>
         <tr>
         <th>ID</th>
         <th>First Name</th>
         <th>Last Name</th>
         <th>Username</th>
         <th>Email</th>
         </tr>

    ");

    while($row = mysqli_fetch_array($result))
    {
        echo(
            "<tr>" .
            "<td>" . $row['id']. "</td>".
            "<td>" . $row['fname'] . "</td>".
            "<td>" . $row['lname'] . "</td>".
            "<td>" . $row['username'] . "</td>".
            "<td>" . $row['email'] . "</td>".
            "</tr>"

        );
    }


    echo "</table>";
}

非常新的php,我觉得答案非常简单,任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

也许就是那个“用户”而不是用户。试试看是否解决。如果没有尝试此代码:

[TRY将函数的内容保存在另一个文件中,只需进行回显以查看函数是否被调用]

public function getUsers(){
    global $connectstr_dbhost, $connectstr_dbname, $connectstr_dbpassword, $connectstr_dbusername;
    $link = mysqli_connect($connectstr_dbhost, $connectstr_dbusername, $connectstr_dbpassword,$connectstr_dbname) or die('Não foi possível conectar');
    $sql = "SELECT * FROM users";
    $result = $link->query($sql);

    echo "
         <table border='1'>
         <tr>
         <th>ID</th>
         <th>First Name</th>
         <th>Last Name</th>
         <th>Username</th>
         <th>Email</th>
         </tr>

    ";

    while($row = $result->fetch_assoc()) {
        echo "<tr>" .
                "<td>" . $row['id']. "</td>".
                "<td>" . $row['fname'] . "</td>".
                "<td>" . $row['lname'] . "</td>".
                "<td>" . $row['username'] . "</td>".
                "<td>" . $row['email'] . "</td>".
            "</tr>";
    }

    echo "</table>";
}

答案 1 :(得分:0)

想出来,问题是我没有导入数据库类。