Select data with MySQLi (Object-oriented)


SUBMITTED BY: henry1874w

DATE: June 21, 2017, 11:49 p.m.

FORMAT: Text only

SIZE: 724 Bytes

HITS: 237

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <?php
  5. $servername = "localhost";
  6. $username = "username";
  7. $password = "password";
  8. $dbname = "myDB";
  9. // Create connection
  10. $conn = new mysqli($servername, $username, $password, $dbname);
  11. // Check connection
  12. if ($conn->connect_error) {
  13. die("Connection failed: " . $conn->connect_error);
  14. }
  15. $sql = "SELECT id, firstname, lastname FROM MyGuests";
  16. $result = $conn->query($sql);
  17. if ($result->num_rows > 0) {
  18. // output data of each row
  19. while($row = $result->fetch_assoc()) {
  20. echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"] . "<br>";
  21. }
  22. } else {
  23. echo "0 results";
  24. }
  25. $conn->close();
  26. ?>
  27. </body>
  28. </html>

comments powered by Disqus