Where id = xxx or xxx or xxx

petewinter
petewinter used Ask the Experts™
on
How do I write the following...

See code below:

$query_rs_spares_also = "SELECT * FROM spares WHERE id = ".$row_rs_spares['also_purchased1'];

I want to say where the id is equal to $row_rs_spares['also_purchased1'] or $row_rs_spares['also_purchased2'] or $row_rs_spares['also_purchased3']
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Hello petewinter,

You could try

$query_rs_spares_also = "SELECT * FROM spares WHERE id in (" . $row_rs_spares['also_purchased1'] . "," . $row_rs_spares['also_purchased2'] . "," . $row_rs_spares['also_purchased3'] . ")";

You could also try a loop if you have more than 3 elements....

Regards,

alain34
$query_rs_spares_also = "SELECT * FROM spares WHERE id IN (".$row_rs_spares['also_purchased1'].", ".$row_rs_spares['also_purchased2'].", ".$row_rs_spares['also_purchased13].")";
leakim971Multitechnician
Top Expert 2014
Commented:
Hello petewinter,

Try :


SELECT * FROM spares WHERE id IN(".$row_rs_spares['also_purchased1'].",".$row_rs_spares['also_purchased2'].",".$row_rs_spares['also_purchased3'].")";

Open in new window

Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Commented:
If the number of values is not defined you can do:
$query = "SELECT * FROM spares WHERE id IN(".implode(",",$row_rs_spares).")";

Author

Commented:
Thanks you all for your reply, but...

alain34 & leakim971 I get the following error message:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

ludofulop

I get a blank page.

Please can you check my code attached.
mysql_select_db($database_conn_tech8, $conn_tech8);
$query_rs_spares_also = "SELECT * FROM spares WHERE id IN(".$row_rs_spares['also_purchased1'].",".$row_rs_spares['also_purchased2'].",".$row_rs_spares['also_purchased3'].")";
$rs_spares_also = mysql_query($query_rs_spares_also, $conn_tech8) or die(mysql_error());
$row_rs_spares_also = mysql_fetch_assoc($rs_spares_also);
$totalRows_rs_spares_also = mysql_num_rows($rs_spares_also);

Open in new window

Multitechnician
Top Expert 2014
Commented:
Please confirm xxx is number or try :



SELECT * FROM spares WHERE id IN('".$row_rs_spares['also_purchased1']."','".$row_rs_spares['also_purchased2']."','".$row_rs_spares['also_purchased3']."')";

Open in new window

Author

Commented:
Thanks Leakim971 My error occurred when of the variables was null.
leakim971Multitechnician
Top Expert 2014

Commented:
You're welcome! Thanks for the points!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial