little help using an array IN a query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
glennnphp
Forum Newbie
Posts: 16
Joined: Thu Aug 28, 2008 10:13 pm

little help using an array IN a query

Post by glennnphp »

I don't mind if someone could point me to the right tutorial, but i haven't had any luck finding one for this particular issue (either way, i could use a little help):

i've got an array, $offering, which would return something like 03, 04, 08

foreach ($offering as $f) {
$deg = $f;
echo $deg; // gives me 030408 - fine...
}

while

foreach ($offering as $f) {
$deg = $f;
}
echo $deg; // only gives me 08 - so i can only assume that

$result = mysql_query("SELECT COUNT(*) FROM administrators WHERE fice IN (SELECT fice FROM characteristics WHERE carnegie_code = '$carn_class' AND offering = '$deg') ") or die(mysql_error());

is only going to return those with fields 'offering' containing 08...

can someone please show me how to search for ALL the values for that field...?

i really appreciate it...
GN
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: little help using an array IN a query

Post by andyhoneycutt »

the reason why your second one is only printing the "08" value is because you're setting $deg to the value of $f, and setting it each time you loop through. The only reason why this looks different in the first scenario is because you are echoing the value of $deg (which is the value of $f, still) inside the loop. You'll need to concatenate to $deg if you wish to keep adding to the value of $deg. You can do this by saying

Code: Select all

$deg .= $f;
The "dot equals" operator appends string values.

I think this may work for you better:

Code: Select all

$offering_sql = implode(',',$offering);
$sql = "SELECT COUNT(*) FROM administrators WHERE fice IN (SELECT fice FROM characteristics WHERE carnegie_code = '$carn_class' AND offering IN ($offering_sql)) ";
That will output something like: SELECT COUNT(*) FROM administrators WHERE fice IN (SELECT fice FROM characteristics WHERE carnegie_code = '<some value>' AND offering IN (08,06,04))
glennnphp
Forum Newbie
Posts: 16
Joined: Thu Aug 28, 2008 10:13 pm

Re: little help using an array IN a query

Post by glennnphp »

awesome -

$offering_sql = implode(',',$offering);

that's what i wasn't sure of...

and the IN thingie.


thanks much, i've got about 7 more to include in this query... that helped a lot. would help more if i could remember it...
Post Reply