php mysqli check if any result exist -
i have code check user data exist in mysql mysqli :
$sql = "select users.email,users.handle,userprofile.mobile users,userprofile users.email =? or users.handle =? or userprofile.mobile=?"; if ($stmt = $mysqli->prepare($sql)) { $stmt->bind_param("sss", $email,$username,$mobile); $stmt->execute(); if($stmt->num_rows){ $result = $stmt->get_result(); $row = $result->fetch_array(mysqli_num); if($row[0] ==$email){echo 'email exist';} if($row[1] ==$username){echo 'username exist';} if($row[2] ==$mobile){echo 'mobile exist';} } else{ echo 'ok'; }
if works when user data exist. if user data not exist , else
not work! why?
the reason occurring because if statement returning true, therefore executes code within it. $stmt->num_rows
can return true
, false
or int
, if no rows have been affected, still returning valid value
0 valid value, if statement skip part of code if logic returns false, changing code following fix issue.
if( $stmt->num_rows > 0) { // code here }
Comments
Post a Comment