My goal is to populate my HTML table with SQL results that I pull from the below PHP code that I have written. Keep in mind that in reality my hostname, username and password variables do have string data in them. The first block is my PHP where I am retrieving my selected data. The second part is where I am trying to populate my HTML table. Unfortunately, I am not seeing any data.

<html lang="en">
  <?php 
    $hostname = "";
    $username = "";
    $password = "";
    $databaseName = "MainDatabase"

    $mysqli = new mysqli($hostname, $username, $password);
    if ($mysqli) {
        $mysql_select_db($databaseName);

        $result = mysql_query("SELECT ID, User_FirstName, User_LastName, User_Email, UserName, UserPassword FROM Users");
        if ($row = mysql_fetch_array($result) {
            if ($query_row = mysql_fetch_assoc($row) {
                $id = $query_row['ID'];
                $user_lastname = $query_row['User_LastName'];
                $user_firstname = $query_row['User_FirstName'];
                $user_email = $query_row['User_Email'];
                $username = $query_row['UserName'];
                $userpassword = $query_row['UserPassword'];
            } else {
              echo "** Error **";
            }
        } else {
          echo "** Error **";
        }
    } else {
      echo "Unable to create connection .. !";
    }
  ?>

   <h2 class="sub-header">Current Registered Users</h2>
          <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>User ID</th>
                  <th>First Name</th>
                  <th>Last Name</th>
                  <th>Primary Email</th>
                  <th>Username</th>
                  <th>Password</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td> <?php echo "$id"; ?> </td>
                  <td> <?php echo "$user_firstname"; ?> </td>
                  <td> <?php echo "$user_lastname"; ?> </td>
                  <td> <?php echo "$user_email"; ?> </td>
                  <td> <?php echo "$username"; ?> </td>
                  <td> <?php echo "$userpassword"; ?> </td>
                </tr>
              </tbody>
            </table>
          </div>

Recommended Answers

All 18 Replies

Member Avatar for diafol

You're confusing mysqli and mysql. You've got both. You've created a mysqli connection and then you're using deprecated mysql_* functions. This wont work.

I updated the following code to use MySQLI functions, but now I am seeing parts of the code showing in my browser. I am guessing this is because I have syntax errors.

 <?php 
    $hostname = "";
    $username = "";
    $password = "";
    $databaseName = "MainDatabase";

    $conn = mysqli_connect($hostname, $username, $password, $databaseName);

    if ($conn) {
        $sql = "SELECT ID, User_FirstName, User_LastName, User_Email, UserName, UserPassword FROM Users";
        $result =  mysqli_query($conn,$sql);

        if (myqli_num_rows($result) > 0) {
          while ($row = mysqli_fetch_assoc($result)) {
            $id = $row['ID'];
            $user_lastname = $row['User_LastName'];
            $user_firstname = $row['User_FirstName'];
            $user_email = $row['User_Email'];
            $username = $row['UserName'];
            $userpassword = $row['UserPassword'];
          }
        } else {
          echo "0 results found in sql query .. !";
        }
    } else {
      echo "Unable to create connection .. !";
    }
  ?>
Member Avatar for diafol

Who knows. But here's a typo:

if (myqli_num_rows($result) > 0) {

Fixed the syntax error, still showing up in my page though ..

Member Avatar for diafol

Can you show a browser screen snapshot and a view source snapshot?

Sure, I will attach those items later tonight. As far as the source code, I will attach my current HTML file.

From my view, i think u should put ur table body inside the while loop,

                <tr>
                  <td> <?php echo $id; ?> </td>
                  <td> <?php echo $user_firstname; ?> </td>
                  <td> <?php echo $user_lastname; ?> </td>
                  <td> <?php echo $user_email; ?> </td>
                  <td> <?php echo $username; ?> </td>
                  <td> <?php echo $userpassword; ?> </td>
                </tr>

Attached are the screenshot and HTML file. For anyone else investigating my issue please remember that I have taken out my server/database credentials

Member Avatar for diafol

Change .html extension to .php

change to .php extension.

Changed the extension .php, but the net result is my page being completely blank .. ?

Member Avatar for diafol

Is Apache turned on? Is PHP installed?

Create a file in the same directory and just have <?php phpinfo(); ?> in it. Run it in the browser - see if anything comes up.

I created that file and I was able to see the PHP Version which is currently installed .. Link -> chaluparosa-gaming/test_phpVersion.php

Member Avatar for diafol

Changed the extension .php, but the net result is my page being completely blank .. ?

That doesn't make much sense. SO you're saying that changing the ext from

developer_cms_dashboard.html to developer_cms_dashboard.php

and running the page in the browser is giving you nothing (nothing shown in View SOurce and console)

I should have been more clearer in my earlier response and I am sorry for that. I checked the page source and the console and all appears is the beginning HTML tag, and the opening/closing Head tag

Member Avatar for diafol

Where you have this:

<?php 
$hostname = "";
$username = "";
$password = "";
$databaseName = "MainDatabase";

$conn = mysqli_connect($hostname, $username, $password, $databaseName);

if ($conn) {
    $sql = "SELECT * FROM Users";
    $result = mysqli_query($conn,$sql);

    if (mysqli_num_rows($result) > 0) {
      echo "$result";
      while ($row = mysqli_fetch_assoc($result)) {
        $id = $row['ID'];
        $user_lastname = $row['User_LastName'];
        $user_firstname = $row['User_FirstName'];
        $user_email = $row['User_Email'];
        $username = $row['UserName'];
        $userpassword = $row['UserPassword'];
      }
    } else {
      echo "0 results found in sql query .. !";
    }
} else {
  echo "Unable to create connection .. !";
}
?>

Just comment it out /* ... */ and add

echo "first";

inside the php tags. ALso where you have:

<tr>
              <td> <?php echo "$id"; ?> </td>
              <td> <?php echo "$user_firstname"; ?> </td>
              <td> <?php echo "$user_lastname"; ?> </td>
              <td> <?php echo "$user_email"; ?> </td>
              <td> <?php echo "$username"; ?> </td>
              <td> <?php echo "$userpassword"; ?> </td>
            </tr>

Comment out with something like this for now:

<td> <?php echo 'me'; //"$id"; ?> </td>

That seemd to do the trick, I put the php code inside my table div tag .. Unfortuantely I am only able to see one record in the table rather than all of my user entries. I am guessing its something wrong with my while statement logic

Member Avatar for diafol

Yes. You're overwriting your row variables on every iteration of the loop. Try this:

$templateRow = <<<ROW
            <tr>
              <td>%d</td>
              <td>%s</td>
              <td>%s</td>
              <td>%s</td>
              <td>%s</td>
              <td>%s</td>
            </tr>
ROW;

Then...

$rows = '';
while ($row = mysqli_fetch_assoc($result)) {
    $rows .= sprintf($templateRow, $row['ID'], $row['User_FirstName'], $row['User_LastName'], $row['User_Email'], $row['UserName'], $row['UserPassword']);
}

Then...

        <table class="table table-striped">
          <thead>
            <tr>
              <th>User ID</th>
              <th>First Name</th>
              <th>Last Name</th>
              <th>Primary Email</th>
              <th>Username</th>
              <th>Password</th>
            </tr>
          </thead>
          <tbody>
            <?=$rows?>
          </tbody>
        </table>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.