SELECT返回mysqli_num_rows给出错误:类mysqli_result的对象无法转换为字符串
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
我有以下功能。 它的目的是检查数据库 如果存在, 它应该使用mysqli_num_rows给我一个 如果它不存在,那么mysqli_num_rows应该返回 我的问题在于分配SELECT结果。
但是当我跑到这一步时,我收到了这个错误: p>
users code>,看看
email code>列中是否有电子邮件。 p>
int 1 code>的结果,我将使用它来向用户显示错误,(抱歉,您的电子邮件已经注册)。 p>
int 0 code>并且代码可以继续。 p>
function checkEmail($ email,$ name){
//连接到DB
$ con = connectDB();
//创建SQL为 run
$ sql =“SELECT email FROM`users` WHERE email ='”。 $ email。 “
”;
//运行查询
$ emailExists = mysqli_query($ con,$ sql);
//使用mysqli_num_rows返回查询的行数
$ num_rows = mysqli_num_rows ($ emailExists);
var_dump($ num_rows);
//如果列中没有电子邮件
if($ num_rows == 0){
//返回将在别处使用
返回$ emailExists;
} else {
//发送到错误页面并暂停脚本
标题('位置:error.php?error = emailexists& name ='。$ name);
返回false;
} \ n mysqli_close($ con);
}
code> pre>
类mysqli_result的对象无法转换为字符串
code> pre>
div>
$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