I have the following function. It's aim is to check the database users
and see if the email is present in the email
column.
If it is present, it should give me a result of int 1
using mysqli_num_rows which I'll use to show the user an error, (sorry, your email is already registered).
If it isn't present, then mysqli_num_rows should return int 0
and the code can continue.
My problem is with this assigning of the SELECT result.
function checkEmail($email,$name){
// Connects to DB
$con = connectDB();
// Creates the SQL to be run
$sql = "SELECT email FROM `users` WHERE email = '" . $email . "'";
// Runs the query
$emailExists = mysqli_query($con,$sql);
// Uses mysqli_num_rows to return the number of rows with the query
$num_rows = mysqli_num_rows($emailExists);
var_dump($num_rows);
// if no email exists in column
if ($num_rows == 0){
// return this to be used elsewhere
return $emailExists;
}else{
// send to error page and halt script
Header('Location:error.php?error=emailexists&name=' . $name);
return false;
}
mysqli_close($con);
}
But when I run this far, I get this error:
Object of class mysqli_result could not be converted to string
$emailExists
is a mysqli_result object
When you use it elsewhere, you are expecting it to be a string. Try doing
return mysqli_fetch_array($emailExists);
instead of
return $emailExists;
This will return an array, so to access the record you want to use $returnedvalue[0]['column_name']
to access your data