Page 1 of 1

little help using an array IN a query

Posted: Mon Sep 08, 2008 12:20 pm
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

Re: little help using an array IN a query

Posted: Mon Sep 08, 2008 12:27 pm
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))

Re: little help using an array IN a query

Posted: Mon Sep 08, 2008 1:13 pm
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...