humanware

prepared_statement.php

Sep 18th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. <?php
  2.  
  3. include_once('includes/dbconnection.php');
  4.  
  5. $data = 2;
  6.  
  7. // Creating A Template
  8. $sql = "SELECT * FROM users WHERE user_id = ?";
  9.  
  10. // Creating a prepared statement
  11. $stmt = mysqli_stmt_init($conn);
  12.  
  13. // Prepare the prepared statement
  14. if(!mysqli_stmt_prepare($stmt, $sql)) {
  15. echo 'SQL Statement Failed';
  16. } else {
  17. //Bind parameters to the placeholder
  18. mysqli_stmt_bind_param($stmt, 's', $data);
  19.  
  20. // Run Parameters inside database
  21. mysqli_stmt_execute($stmt);
  22.  
  23. $result = mysqli_stmt_get_result($stmt);
  24.  
  25. while($row = mysqli_fetch_assoc($result)) {
  26. echo $row['name'] . '<br />';
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment