Proper Query Syntax?

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
virgil
Forum Commoner
Posts: 59
Joined: Thu Jun 13, 2002 11:43 pm
Location: New York, U.S.

Proper Query Syntax?

Post by virgil »

Hey PHP's (peeps? MyPHP's LOL),

This code is supposed to return event listings based on multiple months selected from a dropdown menu named "month_filter[]"from a database. With (JAN,FEB,MAR and APR) being the selected months.
//Comments are not in the actual code.

What's wrong with the "SELECT,WHERE,IN "SQL syntax? I get the error:


MySQL error 1064: You have an error in your SQL syntax near 'JAN'.'FEB'.'MAR'.'APR'')' at line 3


Code: Select all

if($date_filter!=$ALL)
{
$count = count($date_filter);

echo"$count"; //Just to test: (Returns 4)
}


if( $count > 1 )
 
   {
   $month_filter = implode( "'.'", $month_filter);

   $month_filter="'$month_filter'"; // This adds quotes at both ends

echo "$month_filter"; //Just to test:(Returns 'JAN'.'FEB'.'MAR'.'APR')



     $query="SELECT eventnum, date_year, date_month, date_day 
FROM eventpost WHERE date_year='$year_filter' AND date_month IN ('$month_filter')";
   


$result = mysql_query($query);
   
    while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) etc.....

Whats wrong?

Thanks for any help.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You need commas not fullstops between the information in the IN() bit. Change,

Code: Select all

$month_filter = implode( "'.'", $month_filter);
to

Code: Select all

$month_filter = implode( "','", $month_filter);
Mac
User avatar
roninblade
Forum Newbie
Posts: 21
Joined: Thu Jun 13, 2002 7:12 pm

Post by roninblade »

you're imploding your array with a dot , try imploding it with a comma.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Umm, that's exactly what I said... :roll:

Mac
User avatar
roninblade
Forum Newbie
Posts: 21
Joined: Thu Jun 13, 2002 7:12 pm

Post by roninblade »

errr... i didnt see you posted before me. :D :P
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Aah, the hazard of taking time to actually think about what you're going to say... :lol:

Mac
User avatar
roninblade
Forum Newbie
Posts: 21
Joined: Thu Jun 13, 2002 7:12 pm

Post by roninblade »

yep! i fell asleep while working. :oops: :D
virgil
Forum Commoner
Posts: 59
Joined: Thu Jun 13, 2002 11:43 pm
Location: New York, U.S.

Post by virgil »

Worked like a charm.....Thanks again ...Don't know what I would do with out ya.... Virgil :D
Post Reply