user1677709
user1677709

Reputation: 45

mysql_query always returns false with SELECT EXISTS

I'm trying to check either if a row exists or not, using the EXISTS syntax. The connect() function is working (at least in all other functions), but the this function always returns false. The query gives me true or false when trying thru phpMyAdmin. So to my question; how to read the answer with php? I don't want to use a normal SELECT statement because I've read some benchmarking tests witch says this method is faster.

function isOnline($userid){
    $conn = connect();
    $query = "SELECT EXISTS (SELECT 1 FROM `usersonline` WHERE userid=$userid);";
    return mysql_query($query, $conn) ? false : true;
}

Upvotes: 0

Views: 1380

Answers (3)

Guru
Guru

Reputation: 664

function isOnline($userid){
    $conn = connect();
    $query = mysql_query("SELECT `id` FROM `usersonline` WHERE userid=$userid");
    $row = mysql_num_rows($query)
    return if(row>=1) ? true : false;
}

You Can Use This

Upvotes: 0

jeroen
jeroen

Reputation: 91734

Your function always returns false because your query is always successful.

You need to get a result from your query's result set and test that.

Upvotes: 0

Sabeen Malik
Sabeen Malik

Reputation: 10880

The mysql_query function only indicates if the query executed successfully. You need to read the first row of the data returned, maybe with mysql_fetch_row and figure out if its giving you true or false.

Also please understand that mysql_* functions are now deprecated and you should move on to newer PDO or mysqli_* functions

Upvotes: 3

Related Questions