0

I currently have a mysql_query line that looks like this:

mysql_query("SELECT * from users WHERE id = '". $id1 ."' ") or die(mysql_error());

However, I want to select everything from users where id = $id1 OR $id2. How would I edit this line to do so?

I've thought about just having two copies of this line, one with $id1 and one with $id2. However, I'm sure that there is a more efficent way to accomplish this. I'm very new to SQL.

user2898075
  • 79
  • 1
  • 3
  • 10

3 Answers3

0

The answer is almost in your question. You should change the query into this:

mysql_query("SELECT * from users WHERE id = '". $id1 ."' OR id = '" . $id2. "'")

You should however not be using the mysql_* functions anymore, since they are deprecated. Use mysqli_* or PDO instead.

Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46
0

You can use the IN clause just like the example below.

SELECT * FROM employees WHERE id IN ( 250, 220, 170 );
-1

Your query statement can be something like

$query = "SELECT * FROM users WHERE id IN('{$id1}','{$id2}')";

or,

$query = "SELECT * FROM usrs WHERE id = '{$id1}' or id = '{$id2}'";

mysql_* is officially deprecated, so use mysqli or PDO to query your statements.

nurakantech
  • 502
  • 4
  • 14