Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

SQL Select question...


Go to End


2 Posts   1790 Views

Avatar
Skullies

Community Member, 19 Posts

30 August 2016 at 8:18pm

Edited: 30/08/2016 9:15pm

Good day.
I am having trouble finding the right way to do a addwhere with a array of items ie. ID's
my code looks as follow

	$ItemID = $_POST['ItemID'];
	$sqlQuery = new SQLSelect();
	$sqlQuery->setFrom('products');
	$sqlQuery->selectField('ModelNo', 'ModelNo');
	$sqlQuery->selectField('ImageURL');
	$sqlQuery->selectField('Manufacturer', 'Manufacturer');
	$sqlQuery->selectField('RetailPrice','RetailPrice');
	$sqlQuery->addWhere(array('ID' => $ItemID));
	$Amounts = $sqlQuery->execute();

As the code is it works for getting the information from 1 ID but if i have say 2 or 3 ID's in there it does not work
I want to know is there a better way to do the addWhere where i can have multiple ID's which will call their respectable fields?

Any help will be appreciated
Thanks
Skullies

Avatar
ZarockNZ

Community Member, 17 Posts

18 October 2016 at 11:41am

The following should work if ItemID is an array of IDs....

$sqlQuery->addWhere(array('ID IN (?)' => implode(",", $ItemID)));

https://www.tutorialspoint.com/mysql/mysql-in-clause.htm
http://php.net/manual/en/function.implode.php