Jump to content

Multiple select from database where clause


adamf

Recommended Posts

Hi,

 

I have a website with a database full of category names...I want to be able to chose lets say 5 of these categories and use a select from database clause where I could simply enter the category names I want to use but am not sure how I would go about doing it.

 

I can get the list to display just the Aprilia category using the code below:

 

$data1->q("SELECT * FROM categories WHERE cat_name = 'Aprilia' ORDER BY cat_name asc");

 

However...how could I get my site to display Aprilia, Ducati, Honda, Suzuki, Yamaha?

 

Adam

Link to comment
Share on other sites

Use the implode() function on an array to create a list of possible values to use with the IN syntax in your query:

 

<?php
$cats = array('Aprilia', 'Ducati', 'Honda', 'Suzuki', 'Yamaha');//an array of the possible categories. I assume this might depend on form input. 
$data1->q("SELECT * FROM categories WHERE cat_name = IN(."implode(',',$cats)".) ORDER BY cat_name asc");
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.