简体   繁体   中英

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] ). Instead of validating the query result, you are validating the query condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM