Help with MySQL Select statement in PHP code

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
satcom
Forum Newbie
Posts: 2
Joined: Sun Feb 03, 2008 3:16 pm

Help with MySQL Select statement in PHP code

Post by satcom »

I've got the following SQL statement that I'm wanting to customize for a website I'm working on.

$query="SELECT * FROM #__alberghi_facilities WHERE id IN (".$item->facilities.")";

Basically, I'm wanting to not include certain id's in the query.

For example, lets say that $item->facilities included 3, 5, 8, 9 and I wanted to exclude 8 from the query.

I want the select to pull the records except 5 and 9. I know that an and statement is needed but I'm not sure how to do it.

Any help would be appreciated.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help with MySQL Select statement in PHP code

Post by Christopher »

Are you asking how to use the IN() operator or how to remove numbers from $item->facilities? If the latter, is $item->facilities as string or array?
(#10850)
satcom
Forum Newbie
Posts: 2
Joined: Sun Feb 03, 2008 3:16 pm

Re: Help with MySQL Select statement in PHP code

Post by satcom »

It is an array. I'm wanting to exclude certain results from the query.

This is what I was thinking I just don't know how to write it.


$query="SELECT * FROM #__alberghi_facilities WHERE id IN (".$item->facilities.") and id isnot '8' ";

I know that is written incorrectly. Also there are times when several id's need to be excluded. Actually it is always going to be the same id's that are excluded.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Help with MySQL Select statement in PHP code

Post by Christopher »

Have you looked at the PHP documentation for arrays:

http://www.php.net/manual/en/ref.array.php

There are functions to do all sorts of things to arrays (no pun intended ;)).
(#10850)
Post Reply