|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
MySQL SELECT statement error
I am having a problem with a MySQL Query:
PHP Code:
And I get this error Code:
You have an error in your SQL syntax near 'WHERE
(article_type=('1'||'2'||'3'||'4'||'5'||'6'))&&(year='2003') ORDER BY
post' at line 1
I have also had the WHERE statement as Code:
((article_type='$type1')||(article_type='$type2')| |etc.......)&& (year='$year') I do not know why it is giving me the error. is my SQL stememnt too long, am I doing something stupid with my statement like adding a semicolon or something?
__________________
CHornJr "One day I'll know what I am doing" ![]() My Blog Suanhacky Lodge #49 Rebel Squadrons |
|
#2
|
||||
|
||||
|
To my knowledge, you can't group a bunch of OR values within parentheses as you've done in your initial example. I believe you have to always use the x=y syntax.
The main problem is that you've left off the "FROM table" portion of your statement, though. |
|
#3
|
|||
|
|||
|
You could solve this by using MYSQL's IN() function. Just change the WHERE clause to this:
WHERE (article_type IN ('$type1', '$type2', '$type3', '$type4', '$type5', '$type6')) && (year='$year') The IN() function basically allows you to give a list of acceptable values for a variable. In the above case it will return true if the 'article_type' field is equal to $type1, $type2, $type3, etc... The above post is also correct in that you will need a "FROM table" placed in your query ie: SELECT article_id,article_title,UNIX_TIMESTAMP(posting_da te) as posting_date FROM articles WHERE <insert rest of statement here> Hope that helps ![]() |
|
#4
|
||||
|
||||
|
Thank You for pointing outmy stupid mistake. I forgot teh FROM table.
|
|
#5
|
|||
|
|||
|
If you still are coming up with any logic errors I would still strongly suggest you use that IN function. If I am not mistaken the "article_type" section of your WHERE clause:
Code:
article_type = ('$type1'||'$type2'||'$type3'||'$type4'||'$type5'|
|'$type6')
Will almost always evaluate to false unless article_type = 1. It's either that or it always evaluates to true unless article_type = 0. Point being if you want to check that a field equals one of many strings you will want to use the IN() function |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > MySQL SELECT statement error |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|