Advertisement
Guest User

PHP connection

a guest
Jan 24th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. // PHP pages are server side so it can't be examinated from the user on the browser
  2.  
  3. <?php
  4. $host = "ip:port"; //port might not be needed depending on your server
  5. $user = "username"; //username to access the DB
  6. $password = "password"; //leave it blank if there's no password
  7. $db = "databaseName"; //name of the DB
  8. $conn = mysqli_connect($host, $user, $password, $db); //create connection
  9.  
  10. if (!$conn) {
  11. die("Connection error: " . mysqli_connect_errno());
  12. } else {
  13. echo "Connection was succesfully!"; // or if you have the HTML code in the same page, you can delete the else statement
  14. }
  15. ?>
  16.  
  17. <html> //open HTML
  18. <head>
  19. <link rel="stylesheet" type="text/css" href="indexstyle.css"> //link to CSS
  20. </head>
  21. <body>
  22. <div id="container"> //create a div so you can modify the look on CSS
  23. <table> //creating table and the column and rows for your data output
  24. <tr>
  25. <th>ID</th>
  26. <th>Name</th>
  27. <th>Surname</th>
  28. </tr>
  29.  
  30. <?php //re open PHP to create the query and ge the result
  31. $sql = "SELECT ID, name, surname FROM tableName"; //insert your SQL query here
  32. $result = $conn->query($sql);
  33.  
  34. if ($result->num_rows > 0) { // output data of each row
  35. while($row = $result->fetch_assoc()) {
  36. $str = "<tr><td>". $row['ID']. "</td><td>". $row['name']. "</td><td>". $row['surname']. "</td></tr>"; //The output is set to a VAR, you can also directly do an ECHO on it
  37. echo strtoupper($str); //this makes everything in capital letters
  38. }
  39. echo "</table>"; //closing table
  40. } else {
  41. echo "Nothing found!"; //Something else
  42. }
  43. mysqli_close($conn); //remember to close the connection
  44. ?>
  45.  
  46. </body>
  47. </html> //close HTML
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement