Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. <?php
  2. //1. Create a database connection
  3. $dbhost = "localhost";
  4. $dbuser = "root";
  5. $dbpass = "";
  6. $dbname = "test";
  7. $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  8.  
  9. //Showing error in case of failed connection
  10. if(mysqli_connect_errno()){
  11. die(
  12. "Database connection failed: ".mysqli_connect_error()." (".mysqli_connect_errno().")"
  13. );
  14. }
  15. ?>
  16.  
  17. <?php
  18. //2. Perform Database Query
  19. $query = "SELECT * FROM subjects";
  20. $result = mysqli_query($connection, $query);
  21.  
  22. //Test if there is a query error
  23. if(!$result){
  24. die("Database Query Failed");
  25. }
  26. ?>
  27.  
  28. <html>
  29. <head>
  30. <title>PHP MySQL Database</title>
  31. </head>
  32. <body>
  33. <?php
  34. //3. User returned Data (if any)
  35. while($row = mysqli_fetch_row($result)){
  36. //output data from each row
  37. var_dump();
  38. echo "<hr/>";
  39. }
  40. ?>
  41.  
  42. <?php
  43. //4. Release the returned data
  44. mysqli_close($connection);
  45. ?>
  46. </body>
  47. </html>
  48.  
  49. <?php
  50. //5. Close database connection
  51. mysqli_close($connection);
  52. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement