0

I slightly modified the Select Exists function that I found on a stackoverflow answer. How do I access the result of the EXISTS query?

function uniqueexists($dbHandle,$tablename,$fieldname='username',$value) {
    $sql = 'SELECT EXISTS(SELECT * FROM `'.$tablename.'` WHERE `'.$fieldname.'` = ? LIMIT 1)';
    $stmt = mysqli_prepare($dbHandle, $sql);
    mysqli_stmt_bind_param($stmt, 's',$value);  
    $sqlResult = mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    
    $exists = ... // How to use the $sqlResult or the $result? ...

    return $exists;
}

With: username Index varchar(255) utf8_german2_ci
The fieldname's entries are set to unique.

Bill2022
  • 29
  • 9
  • 1
    Execute query without `SELECT EXISTS` and if there is results, then your entity exists – Justinas Feb 16 '22 at 18:27
  • 1
    The result of `EXISTS(...)` is by definition either true or false, there is no other result to access. Do you mean you want to look at the row it found? If so, just don't surround it with `SELECT EXISTS(...)` – IMSoP Feb 16 '22 at 18:28
  • The result of the `EXISTS()` function will be in the first column of the first row. – rickdenhaan Feb 16 '22 at 18:31
  • @rickdenhaan Thank you. I will try that. I simply couldn't find any reference in the documentation to the type of the return value. – Bill2022 Feb 16 '22 at 18:38

0 Answers0