Guest User

var_dump

a guest
Nov 9th, 2016
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.24 KB | None | 0 0
  1. <?php
  2. //1. Create a db connection
  3. $dbhost = "localhost";
  4. $dbuser = "widget_cms";
  5. $dbpass = "secretpassword";
  6. $dbname = "widget_corp";
  7.  
  8. $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  9.  
  10. //Test if connection occured
  11. if(mysqli_connect_errno()) {
  12.     die("Database connection failed: " .
  13.            mysqli_connect_error() .
  14.            " (" . mysqli_connect_errno() . ")"
  15.     );
  16. } else {
  17.     $connection_status = "true";
  18. }
  19. ?>
  20.  
  21.  
  22.  
  23.  
  24. <?php
  25. //2. Perform db query
  26. $query = "SELECT * FROM subjects";
  27. $result = mysqli_query($connection, $query);
  28.  
  29. //Test if there was a query error
  30. if(!$result) {
  31.     die("DB query failed.");
  32. }
  33.  
  34. ?>
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. <!DOCTYPE html>
  47. <html lang="en">
  48. <head>
  49.     <meta charset="UTF-8">
  50.     <title>connecting to mysql with php</title>
  51. </head>
  52. <body>
  53.  
  54.  
  55.  
  56. <?php
  57. if(isset($connection_status)) {
  58.     echo "Connection successfull. <hr />";
  59. }
  60. ?>
  61.  
  62.  
  63. <?php
  64. //3. Use returned data (if any)
  65.  
  66. //while it can fetch assign it to $row variable   (   mysqli_fetch_row($result)   ) - is a condition
  67. while( $row = mysqli_fetch_row($result) ) {
  68.     //output data from each row
  69.     var_dump($row);
  70.     echo "<hr />";
  71. }
  72.  
  73.  
  74.  
  75.  
  76. ?>
  77.  
  78.  
  79.  
  80.    
  81. </body>
  82. </html>
  83.  
  84. <?php
  85. // 5. Close database connection
  86. mysqli_close($connection);
  87. ?>
Add Comment
Please, Sign In to add comment