如何使用 PHP 检查用户是否已存在于 MySQL 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17465468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if user already exists in MySQL with PHP
提问by Prodeep Chatterjee
I am using following code which is not working for me.
我正在使用以下对我不起作用的代码。
$con=mysqli_connect("localhost","root","","my_db");
$check="SELECT COUNT(*) FROM persons WHERE Email = '$_POST[eMailTxt]'";
if (mysqli_query($con,$check)>=1)
{
echo "User Already in Exists<br/>";
}
else
{
$newUser="INSERT INTO persons(Email,FirstName,LastName,PassWord) values('$_POST[eMailTxt]','$_POST[NameTxt]','$_POST[LnameTxt]','$_POST[passWordTxt]')";
if (mysqli_query($con,$newUser))
{
echo "You are now registered<br/>";
}
else
{
echo "Error adding user in database<br/>";
}
}
Object of class mysqli_result could not be converted to int in C:\xampp\htdocs\Exp\welcome.php
mysqli_result 类的对象无法在C:\xampp\htdocs\Exp\welcome.php 中转换为 int
回答by Vijay
this code works fine for you...
这段代码很适合你......
$con=mysqli_connect("localhost","root","","my_db");
$check="SELECT * FROM persons WHERE Email = '$_POST[eMailTxt]'";
$rs = mysqli_query($con,$check);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
if($data[0] > 1) {
echo "User Already in Exists<br/>";
}
else
{
$newUser="INSERT INTO persons(Email,FirstName,LastName,PassWord) values('$_POST[eMailTxt]','$_POST[NameTxt]','$_POST[LnameTxt]','$_POST[passWordTxt]')";
if (mysqli_query($con,$newUser))
{
echo "You are now registered<br/>";
}
else
{
echo "Error adding user in database<br/>";
}
}
回答by Aleks G
mysqli_query
function returns a resultset handle. You then need to read the rows from it:
mysqli_query
函数返回结果集句柄。然后,您需要从中读取行:
$rs = mysqli_query($con,$check);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
if($data[0] > 1) {
//user exists;
}
Also note that SELECT count(1) FROM ...
will be faster than SELECT count(*) FROM ...
You won't see much of a difference in a small table, but with a large table of several hundred thousand rows, the difference may be significant.
另请注意,这SELECT count(1) FROM ...
将比SELECT count(*) FROM ...
您在小表中看不到太大差异要快,但对于包含数十万行的大表,差异可能很大。
回答by Jim
mysqli_query
returns a mysqli_result
object. Rather than comparing this to an integer try using num_rows
:
mysqli_query
返回一个mysqli_result
对象。而不是将其与整数进行比较,请尝试使用num_rows
:
$res = mysqli_query($con,$check);
if($res->num_rows){
//User exists
}
Edit: The above assumed the query was using SELECT *
and this will not work with SELECT COUNT(*)
. Checkout Aleks G's answer.
编辑:以上假设查询正在使用SELECT *
,这不适用于SELECT COUNT(*)
. 结帐亚历克斯 G 的答案。
回答by Riche Tomaroy
You can try the following code:
您可以尝试以下代码:
$query_code = "SELECT COUNT(itemCode) FROM masterData WHERE itemCode='{$itemCode}'";
$result_login = mysqli_query($conn,$query_code);
$anything_found = mysqli_num_rows($result_login);
if($anything_found > 0) {
$formOk = false;
echo "ITEM CODE ALREADY EXISTS! Please try again.";
}