Advertisement
jraavis

Display all students

Apr 12th, 2020
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.22 KB | None | 0 0
  1. <?php
  2.     $db_host = '127.0.0.1';
  3.     $db_user = 'root';
  4.     $db_pass = 'root';
  5.     $db_name = 'college';
  6.     $conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
  7.     if($conn === false){
  8.         die('Error while connecting:' . mysqli_connect_error());
  9.     }
  10.     $post = $_POST;
  11.     $sql = "SELECT * FROM students";
  12.     if($result = mysqli_query($link, $sql)){
  13.         if(mysqli_num_rows($result) > 0){
  14.             echo "<table>";
  15.             echo "<tr>";
  16.                 echo "<th>ID</th>";
  17.                 echo "<th>First Name</th>";
  18.                 echo "<th>Last Name</th>";
  19.                 echo "<th>Email</th>";
  20.             echo "</tr>";
  21.             while($row = mysqli_fetch_array($result)){
  22.                 echo "<tr>";
  23.                     echo "<td>" . $row['id'] . "</td>";
  24.                     echo "<td>" . $row['first_name'] . "</td>";
  25.                     echo "<td>" . $row['last_name'] . "</td>";
  26.                     echo "<td>" . $row['email'] . "</td>";
  27.                 echo "</tr>";
  28.             }
  29.             echo "</table>";
  30.             mysqli_free_result($result);
  31.         } else {
  32.             echo "Sorry! No records found.";
  33.         }
  34.     } else {
  35.         echo "Error while executing $sql. " . mysqli_error($link);
  36.     }
  37.  
  38.     // Close connection
  39.     mysqli_close($link);
  40.    
  41. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement