Jump to content

Can WHERE check for multiple values passed from a variable or array?


Recommended Posts

This is my first post so hello and I hope after reading some of the guidelines I get it right.

 

I've got a limited but functional idea of how to have MYSQL use a variable in a query.

 

Is it possible to have MYSQL check for mutliple values as in an array?

 

Below is the query. I'm sure the code could be improved any any help appreciated, but what I really want to be able to do is have the variable $association, which now is created from id field from a radio button that only allows one choice, become a check box that allows multiple selections. How would I have the MYSQL statement written to check against multiple values in $association?

 

Select TrialListing.listingID, BeagleClub.clubName AS Club, CONCAT_WS('-', DATE_FORMAT(TrialListing.dateBegin, ('%m/%e')), DATE_FORMAT(TrialListing.dateEnd, ('%m/%e'))) as D, TrialCategory.categoryDesc AS TrialType, CONCAT_WS(' on ', Format.formatLong, Game.game)as Format, 

CONCAT('$', TrialListing.entryCostID) AS Entry, Prize.prizeName AS Awards, TrialListing.notes AS Comments

FROM  TrialListing, BeagleClub, TrialCategory, Format, Game, Prize

Where TrialListing.clubID = BeagleClub.clubID

AND TrialListing.categoryID = TrialCategory.categoryID

AND TrialListing.formatID = Format.formatID

AND TrialListing.gameID = Game.gameID

AND TrialListing.prizeID = Prize.prizeID

AND TrialListing.dateEnd >= $date

AND BeagleClub.clubAssociation = '$association'

AND TrialListing.categoryID = '$trial_type'

ORDER BY TrialListing.dateBegin, TrialListing.dateEnd

 

If you wish it to be one of several values you can use syntax along the lines of...

 

WHERE field IN (value1, value2, value3)

 

If you create the checkboxes as an array (by naming them all 'something[]') you can easily convert the selected values into this format using...

 

$association = "'" . implode("','", $_POST['something']) . "'";

So your query would then simply be...

 

 Select TrialListing.listingID, BeagleClub.clubName AS Club, CONCAT_WS('-', DATE_FORMAT(TrialListing.dateBegin, ('%m/%e')), DATE_FORMAT(TrialListing.dateEnd, ('%m/%e'))) as D, TrialCategory.categoryDesc AS TrialType, CONCAT_WS(' on ', Format.formatLong, Game.game)as Format,  
CONCAT('$', TrialListing.entryCostID) AS Entry, Prize.prizeName AS Awards, TrialListing.notes AS Comments
FROM  TrialListing, BeagleClub, TrialCategory, Format, Game, Prize
Where TrialListing.clubID = BeagleClub.clubID
AND TrialListing.categoryID = TrialCategory.categoryID
AND TrialListing.formatID = Format.formatID
AND TrialListing.gameID = Game.gameID
AND TrialListing.prizeID = Prize.prizeID
AND TrialListing.dateEnd >= $date
AND BeagleClub.clubAssociation IN($association)
AND TrialListing.categoryID = '$trial_type'
ORDER BY TrialListing.dateBegin, TrialListing.dateEnd

Cags, Thank You very much  :D, worked great! Now a secondary question created by the change. What do you do to prevent the MYSQL error if a user submits the form with all boxes unchecked? I tried the code below but obviously it is wrong.

 

IF (!empty($_POST['association']))			
	 	 $association = $association = "'" . implode("','", $_POST['association']) . "'";
	 		ELSE $association = 'placed default value here';

The code you have is essentially correct, the one thing you are missing is that the string must be surrounded by quotes within the query, so therefore you should probably use "'placed default value here'". Alternatively you could remove the "'" sections from the original code and place them either side of $association within the query.

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.