Joosh
Joosh

Reputation: 136

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:

<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:

    <?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.');
}

?>

Upvotes: 0

Views: 9867

Answers (4)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

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.

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.

Get to work with prepared statements also to help protect against an SQL injection.

Upvotes: 2

Joosh
Joosh

Reputation: 136

Thanks. I have fixed the issue i updated to using mysqli's methods

<?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>";
}
?>

Upvotes: 1

Nuriddin Rashidov
Nuriddin Rashidov

Reputation: 966

You must use mysql_fetch_assoc() instead of mysql_fetch_array or fetch like this mysql_fetch_array($result, MYSQL_ASSOC).

Upvotes: 0

Alex
Alex

Reputation: 17289

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 />';
}

Upvotes: 0

Related Questions