0

I have a php code that displays name and number from a SQL Database. And I have a html table with columns Name and Number. I want the name and number from my SQL database to show up in the HTML table.

The PHP Code

<?php
$servername = "localhost";
$username = "what7aba_wc";
$password = "This is my Password";
$dbname = "what7aba_wc";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT name, number FROM WC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Name" . $row["name"]. "-Number" . $row["number"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

The HTML Table

  <table id="table" class="table table-hover table-mc-light-blue">
      <thead>
        <tr>
          <th>Name</th>
          <th>Link</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td data-title="Name">Its a Name</td>
          <td data-title="Number">

        </tr>
        <tr>
          <td data-title="Name">Its a Name</td>
          <td data-title="Number">

        </tr>
      </tbody>
    </table>

If this can't happen. Anyone tell me how to make a CSS for PHP file given above.

UPDATE:

i've tried this.

<?php
$servername = "localhost";
$username = "what7aba_wc";
$password = "My Password";
$dbname = "what7aba_wc";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT name, number FROM WC";
$result = $conn->query($sql);

?>
  <h1>WhatsCast List</h1>
  <h2>Table of my other Material Design works (list was updated 08.2015)</h2>

  <div class="table-responsive-vertical shadow-z-1">
  <table id="table" class="table table-hover table-mc-light-blue">
      <thead>
        <tr>
          <th>Name</th>
          <th>Link</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td data-title="Name"><p><?php echo $row["name"] ?><p></td>
          <td data-title="Number"><p><?php echo $row["number"] ?></p></td>
         </tr>

      </tbody>
    </table>
  </div>

</div>
1
  • 1
    You have to echo HTML codes Commented Nov 21, 2015 at 12:23

1 Answer 1

2

HTML

<table id="table" class="table table-hover table-mc-light-blue">
  <thead>
    <tr>
      <th>Name</th>
      <th>Link</th>
    </tr>
  </thead>
  <tbody>
    <?php
      while($row = $result->fetch_assoc()) { ?>
     <tr>
      <td data-title="Name"><?php echo $row["name"]?></td>
      <td data-title="Number"><?php echo $row["number"] ?></td>
    </tr>
    <? } ?>
  </tbody>
</table>
Sign up to request clarification or add additional context in comments.

3 Comments

Do I need to specify the Database Details to create connection?
But. It displays the Oldest First and Newer ones in the last. link = whatscast.in/test.php . So, Now I want that do display Newer ones first and Older ones in the last..
is it your sample page? - whatscast.in/test.php May I seeyour latest code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.