SQL problem O.o

Associate
Joined
4 Mar 2007
Posts
315
Location
United Kingdom
I have just gone brain dead, anyone can tell me what is wrong with this code lol.
I dunno but I am overlooking such a stupidly simple thing, coding for several hours straight doesn't help haha.

PHP:
if(mysql_query("SELECT * FROM `calculation` WHERE username='Hobgoblin'")){
echo "duplicate detected";
}
 
mysql_query returns a resource for a SELECT query. You need to extract the information from that query using the appropriate function (i.e. mysql_num_rows in your case).

Strictly speaking, by the way, the correct way to check for a conflict like this is to use COUNT(*) to find the number of rows with the given user name. This avoids actually fetching the row from the database (since it's not needed).
 
You really need to read up on how to use mysql_query...

it's not the fact that I don't know, it's the fact that I can't think as i've been coding for quite some time and need to finish this part asap lol.

doesn't help that I made an abstraction layer too which my guess is it's falling under that region.
 
You are using mysql_query() inside an if() statement which expects a boolean value. mysql_query() returns true on success and false on error which is not the functionality you're after.
 
You are using mysql_query() inside an if() statement which expects a boolean value. mysql_query() returns true on success and false on error which is not the functionality you're after.
It returns a MySQL resource handler on success, and false on failure. A query returning 0 results is a success...
 
Back
Top Bottom