$username = $this->mysqli->real_escape_string( $param['username'] );
$sql = "SELECT * FROM user WHERE username = '{$username}'";
$result = $this->mysqli->multi_query($sql);
if( !$result ){
//username doesnot exist, create new one.
}else{
//username exist
}
The above code is written to check if the username already exist or not!! but whatever the input is, the output will be username exist. Please help.
You're simply checking if the query ran successfully. You want to check for the number of rows returned by the query.
$sql = "SELECT * FROM user WHERE username = '{$username}'";
if($result = $this->mysqli->multi_query($sql))
{
$row_cnt = $result->num_rows;
if($row_cnt > 0){
// username exists
}
else{
// username does not exist
}
}
Also, as some people have suggested above, why are you using multi_query
- did you mean to use query
instead?
From the manual:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
You will need $result->num_rows to get the amount of results
if($result ){
if($result->num_rows == 0){
// user doesn't exist
else{
// user exist
}
}
else{
//error
}