Forum Moderators: open
I'm trying to go as easy on my server as I can.
I'm basically trying to check my server if there is an instance in my table where two things are true.
I'm using the following command:
if(mysql_num_rows(mysql_query("SELECT * FROM table_name WHERE value1='$value1' AND value2='$value2'"))==1){...}
Just looking at this, it seems a little heavy. I'm using a SELECT command, but I really don't need to gain any information, I'm just counting the number of rows that meet my criteria.
The reason I'm trying to go light on my server is that this is the first step to determine how information will be stored. All requests will get this look up, and from this the script will decide how best to handle the user. All transactions on this page will be looked up in my table.
Any modifications or suggestions on how to be nicer to my server is much appreciated.
Thanks
I'm basically trying to check my server if there is an instance in my table where two things are true.
An arguably more efficient method is to use EXISTS (if you don't actually need the count of the rows, and your database supports subqueries), such as :
SELECT 1 FROM dual WHERE EXISTS (SELECT * FROM table WHERE value1=myval1 AND value2=myval2)
Would proper code have this structure:
if(mysql_query("SELECT 1 FROM table_name WHERE EXISTS (SELECT * FROM table_name WHERE value1='$myval1' AND value2='$myval2')")){...}
Your code fragment still looks incorrect - you will need to create the query object first (as has been mentioned), then check if a row has been returned or not.
As far as EXISTS goes - its use is almost exclusive to subqueries.
[dev.mysql.com...]
[edited by: FalseDawn at 11:01 pm (utc) on Dec. 8, 2006]