August 29, 2011

Does MySQL table row contain two values

Question by daidai

I have a table with two columns:

ID1 | ID2
---------
 1  | A
 3  | V
 1  | C
 4  | B
 5  | Q
 1  | S

And I want to be able to find out if any row has, say ID1 = 5 and ID2 = Q , and return a true / false value.

Answer by Starx

Yes, Of course

SELECT * FROM table where ID1='5' and ID2='Q'

PHP (I am just guessing this backend)

$query = "SELECT * FROM table where ID1='5' and ID2='Q'";
$result = mysql_query($query);
if(mysql_num_rows($result)) { return true; }
else { return false; }

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!