<?php
$con = mysqli_connect('localhost','root','[mypassword]','dbhwsource');
if(isset($_GET['username'])){
$username = $con->real_escape_string($_GET['username']);
$test = $con->query("SELECT username FROM users WHERE username='$username'");
if($test!=false) die("usererror");
}
if(isset($_GET['email'])){
$email = $con->real_escape_string($_GET['email']);
$test = $con->query("select * from users where email='$email'");
if($test!=false) die("emailerror");
}
$con->close();
echo "ok";
?>
所以我只是想检查用户名/电子邮件是否可用,但无论输入的用户名是什么,我得到的只是“usererror”!我只是很沮丧,到处搜索示例代码,代码看起来没有任何问题。我做错了什么?
编辑:
$test = $test->fetch_assoc();
if(!empty($test)) die("usererror");
这成功了!
最佳答案
由于您的查询返回 true,这行 if($test!=false) die("usererror");
被执行,
应该是这样的
$test = $con->query("SELECT username FROM users WHERE username='$username'");
$row_cnt = $test->num_rows;
if( $row_cnt > 0 ) {
//you already have user with this name, do something
}
关于php - 检查用户名可用性 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14795815/