0

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>

Edited by Ian_7

3
Contributors
18
Replies
90
Views
3 Years
Discussion Span
Last Post by diafol
0

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.

0

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

Edited by Ian_7

0

Who knows. But here's a typo:

if (myqli_num_rows($result) > 0) {
0

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

0

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

0

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

Edited by Ian_7

0

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

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

Edited by Ian_7

Attachments ScreenGrab1.PNG 47.82 KB
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
      <link rel="icon" href="picture_library/favicon.ico" type="image/ico">

    <title>Dashboard Home Page</title>

   <!-- Bootstrap core CSS -->
    <link href="_bootstrap/css/bootstrap.min.css" rel="stylesheet" />

    <!-- Custom styles for this template -->
    <link href="_CSS/bootstrap_custom_dashboard.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  <?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 .. !";
	}
  ?>

  <body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Main Portal - Home</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Dashboard</a></li>
            <li><a href="#">Settings</a></li>
            <li><a href="#">Profile</a></li>
            <li><a href="#">Help</a></li>
          </ul>
          <form class="navbar-form navbar-right">
            <input type="text" class="form-control" placeholder="Search...">
          </form>
        </div>
      </div>
    </nav>

    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
          <ul class="nav nav-sidebar">
            <li class="active"><a href="deverloper_cms_dashboard.html">Overview <span class="sr-only">(current)</span></a></li>
            <li><a href="developer_cms_databaseAdmin.html">Create Publisher</a></li>
            <li><a href="developer_cms_databaseAdmin_createGame.html">Create Game</a></li>
            <li><a href="developer_cms_databaseAdmin_createDeveloper.html">Create Developer</a></li>
          </ul>
          <ul class="nav nav-sidebar">
            <li><a href="">Nav item</a></li>
            <li><a href="">Nav item again</a></li>
            <li><a href="">One more nav</a></li>
            <li><a href="">Another nav item</a></li>
            <li><a href="">More navigation</a></li>
          </ul>
          <ul class="nav nav-sidebar">
            <li><a href="">Nav item again</a></li>
            <li><a href="">One more nav</a></li>
            <li><a href="">Another nav item</a></li>
          </ul>
        </div>
       
		<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
		  <h1 class="page-header">Dashboard</h1>
          <div class="row placeholders">
            <div class="col-xs-6 col-sm-3 placeholder">
              <img data-src="holder.js/200x200/auto/sky" class="img-responsive" alt="Generic placeholder thumbnail">
              <h4>Label</h4>
              <span class="text-muted">Something else</span>
            </div>
            <div class="col-xs-6 col-sm-3 placeholder">
              <img data-src="holder.js/200x200/auto/vine" class="img-responsive" alt="Generic placeholder thumbnail">
              <h4>Label</h4>
              <span class="text-muted">Something else</span>
            </div>
            <div class="col-xs-6 col-sm-3 placeholder">
              <img data-src="holder.js/200x200/auto/sky" class="img-responsive" alt="Generic placeholder thumbnail">
              <h4>Label</h4>
              <span class="text-muted">Something else</span>
            </div>
            <div class="col-xs-6 col-sm-3 placeholder">
              <img data-src="holder.js/200x200/auto/vine" class="img-responsive" alt="Generic placeholder thumbnail">
              <h4>Label</h4>
              <span class="text-muted">Something else</span>
            </div>
          </div>
		  
          <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>
        </div>
      </div>
    </div>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="../../dist/js/bootstrap.min.js"></script>
    <!-- Just to make our placeholder images work. Don't actually copy the next line! -->
    <script src="../../assets/js/vendor/holder.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
  </body>
</html>
0

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

Edited by Ian_7

0

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.

Edited by diafol

0

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

0

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)

-1

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

Edited by Ian_7

0

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

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

0

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>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.