Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://stackoverflow.com/questions/36838586/print-result-of-prepared-statement-php
- /* ---------------------------------------------------------------------------
- * Mysql connection
- */
- // all errors will throw exceptions - Do not handle them in developement OR testing!
- mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
- $DB_HOST = "localhost";
- $DB_USER = "testmysql";
- $DB_PASSWORD = "testmysql";
- $DB_TO_USE = "testmysql";
- $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_TO_USE);
- //Output any connection error
- if ($mysqli->connect_error) {
- // log_Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
- }
- $user_id = '12321';
- $userRegister = new UserRegister($mysqli);
- $result = $userRegister->isUserRegistered($user_id);
- var_dump('isUserRegistered : '. ($result ? 'true' : 'false'));
- exit;
- /**
- *
- *
- *
- * @return
- */
- class UserRegister {
- protected $conn = null;
- public function __construct($dbConnection)
- {
- $this->conn = $dbConnection;
- }
- public function isUserRegistered($user_id) {
- print $user_id;
- $result_user_id = null;
- $stmt = $this->conn->prepare("SELECT user_id FROM users WHERE user_id = ?");
- $stmt->bind_param("s", $user_id);
- if ($stmt->execute()) {
- $stmt->store_result(); // force num_rows to be updated correctly
- $stmt->bind_result($result_user_id);
- $stmt->fetch();
- // stmt does not have useful 'num_rows' in these circumstances
- $no_of_rows = $stmt->num_rows; // should check against the result_set
- var_dump(__METHOD__,
- 'input user id: '. $user_id,
- 'found user id: '. $result_user_id,
- 'reported number of rows: '. ($no_of_rows), __FILE__.__LINE__);
- $stmt->close();
- if (!empty($result_user_id)) {
- print "Finally";
- // user already registered
- return true;
- } else {
- print "Stupid";
- // user is not registered
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement