Select exists always returns true in my function, but why??

Alles, was PHP betrifft, kann hier besprochen werden.

Select exists always returns true in my function, but why??

Postby Jim2022 » 16. February 2022 15:37

I slightly modified the Select Exists function that I found on a [stackoverflow answer][1], but whatever I try, my following function always gives as `$sqlResult` `true`. Even though I have removed all but two entries in the database.
Does select exists still work?

Code: Select all
    function uniquedoesexist($dbHandle,$tablename,$fieldname,$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);

       echo '$sqlResult: ' . $sqlResult.$br;
       return $sqlResult;
    }


With username Index varchar(255) utf8_german2_ci

[1]: https://stackoverflow.com/questions/1676551/best-way-to-test-if-a-row-exists-in-a-mysql-table/10688065#10688065

Sorry for my beginner question.
The fieldname is set to unique.

Side note:
All the answers the stackoverflow system suggests DO NOT answer the question, but rather suggest using to check the number of returned rows, instead of using exists at all. Also the given links over there for explanations actually do not lead to webpages that do explain what the exists-result does represent.
Jim2022
 
Posts: 2
Joined: 16. February 2022 09:56
XAMPP version: 3.2.4
Operating System: Windows 10

Return to PHP

Who is online

Users browsing this forum: No registered users and 53 guests