seyi
seyi

Reputation: 1

How to select [somename] from a table in my database

The code below will display John, Steve, and Pat from a table in my database. I want it to only display John and Steve.

$result = @mysql_query("SELECT * FROM name");
$line = @mysql_fetch_array($result);
$users = $line[user];

echo "$users";

Upvotes: 0

Views: 146

Answers (5)

David
David

Reputation: 218837

SELECT * FROM name WHERE [column_name] = 'john' OR [column_name] = 'Steve'

Is that what you're looking for? A few notes:

  1. You'll need to replace [column_name] with the name of the actual column being compared, since that's not specified in your code above.
  2. Try not to use SELECT * where possible. It's generally better practice to select the actual column names. Slightly less work on the database, doesn't break as easily when schema changes, etc.
  3. This comparison is case-sensitive.

UPDATE: Correction, MySQL isn't case-sensitive. So #3 above shouldn't be a concern in that environment.

Upvotes: 3

Basic
Basic

Reputation: 26766

$result = @mysql_query("SELECT * FROM name LIMIT 2");

Upvotes: 0

Mike Tunnicliffe
Mike Tunnicliffe

Reputation: 10772

I'm not sure if this is what you want, but you can filter by a list of matching names in the SQL thusly:

SELECT * FROM name WHERE name IN ('John', 'Steve')

This assumes your table is called name and the column containing the user's name is also called name.

I have to wonder what you are trying to achieve here, is there some other useful information you are trying to get from the user record? Or should you be limiting the users returned based on some other criteria beyond their name?

Upvotes: 2

Fosco
Fosco

Reputation: 38516

For this odd example, you could change your query to:

select * from name where name in ('Steve','John')

or

select * from name where name <> 'Pat'

Upvotes: 2

Jan
Jan

Reputation: 2293

You need a WHERE clause in your SQL query.

SELECT * FROM name WHERE somecolumn=somevalue;

Upvotes: 2

Related Questions