-2

I have a table of users and I want a query that will tell me if a particular user is listed in the table. For example, in the table below:

Users | Ages
Bob | 22
Mike | 30
Sue | 21

Can someone help me with a query that does something like,
If (In USERS_TABLE, Mike is in column 'Users'){$userPresent="true";}

Sadie
  • 121
  • 1
  • 7
  • 9
    You really should start with a php / database tutorial or book. – jeroen Dec 11 '13 at 17:12
  • 4
    This is about as elementary as SQL querying gets. Have you actually _tried_ anything? – Matt Ball Dec 11 '13 at 17:12
  • Yeah, I have, but mysql databases aren't something I deal with often, so thanks for not being of any help whatsoever. I can't imagine the burden it must be to know everything you do and spend your time using that knowledge, not to help, but to belittle others. It's people like you that make others not seek answers to their questions. – Sadie Dec 11 '13 at 18:08

5 Answers5

1

Your query would be:

   SELECT * FROM USERS_TABLE WHERE Users='Mike' 
Paul Lo
  • 6,032
  • 6
  • 31
  • 36
1

You could run a query like

SELECT * FROM USERS_TABLE WHERE Users='Mike'

Then, you could check if the number of rows returned from that query was more than 0.

AAA
  • 1,364
  • 1
  • 10
  • 15
1

Mysql Num rows can help you with that

$query = mysql_query("select * from users_table where Users = 'Mike'");


if(mysql_num_rows($query))
{
  echo "user present";//use your code how you want

}
else
{
 echo "user not present";
}

mysql_ extension is deprecated as of PHP 5.5.0. So Please choose PDO or MYSQLI api for better experience.

Manwal
  • 23,450
  • 12
  • 63
  • 93
  • `mysql_` <= deprecated. ;-) – Funk Forty Niner Dec 11 '13 at 18:05
  • @Fred-ii- what exactly you want to say??? "mysql_" deprecated explain it how or when??? – Manwal Dec 11 '13 at 18:13
  • Don't tell me you don't know about this!? How many times this subject comes up, makes my head spin, literally! @Manwal [**Read the RED block on PHP.net about `mysql_` functions**](http://www.php.net/manual/en/function.mysql-num-rows.php) - Plus, its not safe. [**Read this on SO**](http://stackoverflow.com/q/60174/1415724) - If you're still using `mysql_` yourself, I strongly suggest you STOP using it, and change to `mysqli_` with prepared statements, or PDO. And please do not give any more answers using `mysql_` you stand at being told by others also. Just some friendly advice ;-) – Funk Forty Niner Dec 11 '13 at 18:27
0
 SELECT count(*) as count FROM USERS_TABLE WHERE Users='Mike'

If count>0 then user exists.

or if you want that user record,just use as below

 SELECT * FROM USERS_TABLE WHERE Users='Mike'
Haji
  • 1,999
  • 1
  • 15
  • 21
0

Select * FROM USERS_TABLE WHERE Users = 'mike', here you can choose specific table like Select Users FROM USERS_TABLE WHERE Users= 'mike'

Shubham Verma
  • 39
  • 1
  • 12