[英]How do you properly check for an existing data in MongoDB Atlas database using PHP. I always get thet the name already exists
$query = ['name' => $name];
$options = [];
$queryDriver = new MongoDB\Driver\Query($query, $options);
$execute = $conn->executeQuery('db.collection', $queryDriver);
//validation if a user exists
if (!empty($execute)) {
$err['name'] = 'this name already exists';
} else {
echo 'this name doesnt exist';
I am trying to create a register form but i can't validate if a name already exists.我正在尝试创建一个注册表单,但我无法验证名称是否已存在。
Your issue is not connected to the database query result.您的问题与数据库查询结果无关。 In your code, you have in the first line
在您的代码中,您在第一行
$query = ['name' => $name];
and at the end of the script, you are checking在脚本的末尾,您正在检查
if (!empty($query)) {
$query is not empty at this moment (it is ['name' => $name]
). $query 此时不为空(它是
['name' => $name]
)。 Instead of validating the query result, you are validating the query condition.您不是验证查询结果,而是验证查询条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.