我正在尝试从数据库中获取数据并检查返回的结果是否为空。这是我的代码。
<?php
require "init1.php";
$jsonObject = $_GET["UserDetails"];
$obj = json_decode($jsonObject);
$email = $obj->Email;
$password = $obj->Password;
$Username = $obj->Username;
$Sex = $obj->Sex;
$timestamp = strtotime($obj->BirthDay);
$BirthDay = date('Y-m-d', $timestamp);
$sql = "select * from Registered_Users where Username='$Username' or Email='$Email'";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
$myclass = new stdClass();
$myclass->status = "Not Ok";
echo json_encode($myclass);
} else {
try {
$sql_query = "insert into Registered_Users (Email, Password, BirthDay, Sex, Username) values('$email', '$password', '$BirthDay', '$Sex', '$Username')";
if (mysqli_query($con, $sql_query)) {
$obj1 = new stdClass();
$obj1->status = "Ok";
echo json_encode($obj1);
} else {
$obj1 = new stdClass();
$obj1->status = "Not Ok";
}
} catch (Exception $e) {
echo $e->getMessage();
}
}
?>
我能否知道我的上述代码有什么问题,我正在检查数据库中是否存在用户名或电子邮件,如果是,它将打印not ok
否则将打印ok
。但是当我运行代码时,即使$ result大于它的回显ok
,当我第二次运行相同的代码时它会显示NOT OK
。有人可以告诉我什么是错的。
谢谢
答案 0 :(得分:2)
更改变量名称$ Email并使用$ email
$sql = "select * from Registered_Users where Username='$Username' or Email='$email'";