Advertisement
rfv123

36838586/print-result-of-prepared-statement-php

Apr 25th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.26 KB | None | 0 0
  1. // http://stackoverflow.com/questions/36838586/print-result-of-prepared-statement-php
  2.  
  3. /* ---------------------------------------------------------------------------
  4.  * Mysql connection
  5.  */
  6.     // all errors will throw exceptions - Do not handle them in developement OR testing!
  7.     mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  8.  
  9.     $DB_HOST     = "localhost";
  10.     $DB_USER     = "testmysql";
  11.     $DB_PASSWORD = "testmysql";
  12.     $DB_TO_USE   = "testmysql";
  13.  
  14.     $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_TO_USE);
  15.     //Output any connection error
  16.     if ($mysqli->connect_error) {
  17.            // log_Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
  18.     }
  19.  
  20. $user_id = '12321';
  21.  
  22. $userRegister = new UserRegister($mysqli);
  23. $result = $userRegister->isUserRegistered($user_id);
  24. var_dump('isUserRegistered : '. ($result ? 'true' : 'false'));
  25.  
  26. exit;
  27.  
  28. /**
  29. *
  30. *
  31. *
  32. * @return
  33. */
  34. class UserRegister {
  35.    
  36.     protected $conn = null;
  37.    
  38.     public function __construct($dbConnection)
  39.     {
  40.         $this->conn = $dbConnection;
  41.     }  
  42.  
  43.     public function isUserRegistered($user_id) {
  44.         print $user_id;
  45.         $result_user_id = null;
  46.         $stmt = $this->conn->prepare("SELECT user_id FROM users WHERE user_id = ?");
  47.         $stmt->bind_param("s", $user_id);
  48.         if ($stmt->execute()) {
  49.             $stmt->store_result(); // force num_rows to be updated correctly
  50.             $stmt->bind_result($result_user_id);
  51.             $stmt->fetch();    
  52.    
  53.             // stmt does not have useful 'num_rows' in these circumstances
  54.             $no_of_rows = $stmt->num_rows; // should check against the result_set
  55.                
  56.             var_dump(__METHOD__,
  57.                     'input user id: '. $user_id,
  58.                     'found user id: '. $result_user_id,
  59.                     'reported number of rows: '. ($no_of_rows),  __FILE__.__LINE__);
  60.  
  61.             $stmt->close();
  62.             if (!empty($result_user_id)) {
  63.                 print "Finally";
  64.                 // user already registered
  65.                 return true;
  66.             } else {
  67.                 print "Stupid";
  68.                 // user is not registered
  69.                 return false;
  70.             }
  71.         }
  72.     }    
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement