MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 10th, 2003, 10:45 AM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 236 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 50 m 36 sec
Reputation Power: 7
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
MySQL SELECT statement error

I am having a problem with a MySQL Query:

PHP Code:
 $newsquery=mysql_query("SELECT
article_id,article_title,UNIX_TIMESTAMP(posting_da  te) as
posting_date WHERE (article_type=
('$type1'||'$type2'||'$type3'||'$type4'||'$type5'|  |'$type6'))&&
(year='$year') ORDER BY posting_date DESC"


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')
Which gives a similar error.
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

Reply With Quote
  #2  
Old October 10th, 2003, 11:08 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
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.

Reply With Quote
  #3  
Old October 10th, 2003, 03:00 PM
mjlivelyjr mjlivelyjr is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Idaho, US
Posts: 9 mjlivelyjr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old October 10th, 2003, 05:04 PM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 236 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 50 m 36 sec
Reputation Power: 7
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
Thank You for pointing outmy stupid mistake. I forgot teh FROM table.

Reply With Quote
  #5  
Old October 10th, 2003, 05:27 PM
mjlivelyjr mjlivelyjr is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Idaho, US
Posts: 9 mjlivelyjr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > MySQL SELECT statement error


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT