简体   繁体   English

表中来自SQL数据库的PHP列表用户

[英]PHP List Users from SQL Database in Table

Hi so im trying to put all of the useres in a database, table into a html table this is what i have: 嗨,我尝试将所有用户放入数据库,将表放入html表,这就是我所拥有的:

<table class="table table-striped">
    <thead>
        <tr>
            <th>UUID</th>
            <th>Full Name</th>
            <th>Email</th>
            <th>Access Key</th>
            <th>Phone Number</th>
            <th>Activated</th>
            <th>Role</th>
        </tr>
    </thead>
    <tbody>

        <?php
        include_once('inc/conf/databaseConnect.php');
        $query = mysql_query("SELECT * FROM list_users ORDER by id");
        while($row = mysql_fetch_array($query)){
            echo "<tr>";
            echo "<td>".$row['uuid']."</td>";
            echo "<td>".$row['firstname'].$rowtwo['lastname']."</td>";
            echo "<td>".$row['email']."</td>";
            echo "<td>".$row['security_key']."</td>";
            echo "<td>".$row['phone_no']."</td>";
            echo "<td>".$row['activated']."</td>";
            echo "<td>".$row['role']."</td>";
            echo "</tr>";
        }
        ?>

    </tbody>
</table>

This dosnt return anything, or any errors. 此命令不返回任何内容或任何错误。 It connects to the database correctly ive checked that its just not retrieving the users. 它已正确连接到数据库,但已检查其是否未检索到用户。

Image of database structure 数据库结构图

databaseConnect.php: databaseConnect.php:

    <?php

//Create Connnection
$sqlLink = mysqli_connect('localhost', 'root', 'classified', 'user_details');

//If Error Connecting
if(!$sqlLink) {
    die('<center><br><h3>Error connecting to servers Database.');
}

?>

After seeing your edit because you did not originally show us which connection method you were using; 在看到您的修改后,因为您最初没有我们显示您使用的是哪种连接方法; the almighty answer here (least the most important one) is that you can't mix different MySQL APIs. 全能的答案(至少是最重要的答案)是您不能混合使用不同的MySQL API。

You need to use the same one from connection to query. 您需要从连接到查询使用相同的查询。

Plus that $rowtwo should be $row as I stated in comments along with my asking about what was inside your databaseConnect.php file. 再加上$rowtwo应该是$row正如我在评论中所说的那样,同时还要一下databaseConnect.php文件中的内容。

Get to work with prepared statements also to help protect against an SQL injection. 开始使用准备好的语句还有助于防止SQL注入。

At least check for errors: 至少检查错误:

if ($query = mysql_query("SELECT * FROM list_users ORDER by id");) {
  ...
} else {
  echo '<b>MySQL error:</b><br>' . mysql_error() . '<br />';
}

您必须使用mysql_fetch_assoc()而不是mysql_fetch_array或像这样的mysql_fetch_array($result, MYSQL_ASSOC)

Thanks. 谢谢。 I have fixed the issue i updated to using mysqli's methods 我已经解决了我更新为使用mysqli方法的问题

<?php
include_once('inc/conf/databaseConnect.php');
$query = $sqlLink->query("SELECT * FROM list_users ORDER by id");
while($row = $query->fetch_array()){
    echo "<tr>";
    echo "<td>".$row['uuid']."</td>";
    echo "<td>".$row['firstname'].$row['lastname']."</td>";
    echo "<td>".$row['email']."</td>";
    echo "<td>".$row['security_key']."</td>";
    echo "<td>".$row['phone_no']."</td>";
    echo "<td>".$row['activated']."</td>";
    echo "<td>".$row['role']."</td>";
    echo "</tr>";
}
?>

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

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