Guest User

Untitled

a guest
Jan 26th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. <?php
  2.  
  3. // The script connects to the database, run the query and display the output on simple paragraphs.
  4.  
  5. // Create function with connection settings (its recommended to put this info into another file)
  6. function connect() {
  7. // Here we try to connect to database with this credentials:
  8. $hostname = 'localhost'; // Your hostname or server name
  9. $username = 'root'; // User name for mysql
  10. $password = ''; // Password for mysql (in this case is emtpy)
  11.  
  12. // Create connection
  13. $link = mysqli_connect($hostname, $username, $password);
  14. // if cannot establish connection...
  15. if (!$link) {
  16. // display this message:
  17. echo "Error trying to connect with database.";
  18. // and exit php script
  19. exit();
  20. }
  21. return $link;
  22. }
  23.  
  24. // Define variable with function to connect to the db
  25. $link = connect();
  26.  
  27. // Define values for query
  28. $dbname = 'database_name'; // A database name
  29. $tblname = 'table_name'; // A table name
  30.  
  31. // Define a query that you want
  32. $query = "SELECT * FROM $dbname.$tblname;";
  33.  
  34. // Condition if cannot run query
  35. if (!$result = mysqli_query($link, $query)) {
  36. // display error message you want
  37. echo "ERROR - Cannot execute query. Please check your data.";
  38. // and exit from the script
  39. exit();
  40. }
  41. // In case query run successfully
  42. else {
  43. // assign value for each row
  44. while ($row = mysqli_fetch_array($result)) {
  45. // and display a paragraph (you can use a div, table, etc), specifying the column name for each result you want to display
  46. // structure example: .$row["column_name"].
  47. echo "<p>COLUMN NAME: ".$row["column_name_1"]."<br>OTHER COLUMN: ".$row["column_name_2"]."</p>";
  48. }
  49. }
  50.  
  51. // Close mysql connection
  52. mysqli_close($link);
  53.  
  54. // and exit the script
  55. exit();
  56.  
  57. ?>
Add Comment
Please, Sign In to add comment