Guest User

Untitled

a guest
Apr 25th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * Check this out!
  5. * localhost/dont_concatenate_strings.php?id=1
  6. */
  7.  
  8. function init_db() {
  9. $dbhost = "localhost";
  10. $dbuser = "root";
  11. $dbpasswd = "1234";
  12.  
  13. $conn = new mysqli($dbhost, $dbuser, $dbpasswd);
  14. if ($conn->connect_error) {
  15. die("Connection failed: " . $conn->connect_error);
  16. }
  17.  
  18. $result = $conn->query("use dupa;");
  19.  
  20. return $conn;
  21. }
  22.  
  23. function select_user($conn, $id) {
  24. $stmt = $conn->prepare("SELECT id, name FROM users WHERE id = ?");
  25. if (!$stmt)
  26. die("DB error $conn->errno $conn->error");
  27. $stmt->bind_param('i', $id);
  28. $ret = $stmt->execute();
  29. return $stmt->get_result();
  30. }
  31.  
  32. function show_selected_user($conn) {
  33. if (!isset($_GET["id"]))
  34. return;
  35.  
  36. $result = select_user($conn, $_GET["id"]);
  37.  
  38. if ($result->num_rows > 0) {
  39. while ($row = $result->fetch_assoc()) {
  40. echo "id: " . $row["id"] . ", name: " . $row["name"] . "<br>\n";
  41. }
  42. } else {
  43. echo "Users table is empty! :O<br>\n";
  44. }
  45. }
  46.  
  47. $conn = init_db();
  48. ?>
  49.  
  50. <html>
  51. <head><title>This is my test page</title></head>
  52. <body>
  53. <?php show_selected_user($conn); ?>
  54. </body>
  55. </html>
  56.  
  57. <?php $conn->close(); ?>
Add Comment
Please, Sign In to add comment