Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
random row from table with WHERE clause
Old 07-30-2008, 03:43 PM random row from table with WHERE clause
Extreme Talker

Posts: 177
Trades: 0
Ok so I realized the other day that I have a table with picture urls in it, but some of those pictures are part of an inactive album, so they can't be displayed.

Unfortunately on the home page I have a random image displayed....which also displays inactive album pictures, so I'm trying to update it to not include those.

currently I have...
PHP Code:
$random_pic mysql_fetch_array(mysql_query("SELECT * FROM pictures ORDER BY RAND() LIMIT 1"); 
but I want it to include
PHP Code:
WHERE AlbumID != '###' && AlbumID != '###' ... 
I have tried implementing that after 'pictures' and after LIMIT 1, but each time it gives me an error saying the supplied argument is invalid.

What's the proper syntax?
kbfirebreather is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-30-2008, 04:39 PM Re: random row from table with WHERE clause
Skilled Talker

Posts: 55
Name: Mike
Trades: 0
I think what you're looking for is AND.

PHP Code:
$random_pic mysql_fetch_array(mysql_query("SELECT * FROM pictures ORDER BY RAND() WHERE AlbumID != '###' AND AlbumID != '###' LIMIT 1"); 
I would recommend adding an "active" field to your table instead of checking the id's. You could then just do,
PHP Code:
WHERE active 'yes' 

Last edited by XBSHX; 07-30-2008 at 04:42 PM..
XBSHX is offline
Reply With Quote
View Public Profile
 
Old 07-30-2008, 05:17 PM Re: random row from table with WHERE clause
Extreme Talker

Posts: 177
Trades: 0
well I was thinking about that, but what I have is an albums table with and activeAlbum column in it, which I planned on using. Find the inactive album, get their id's, etc.

so I'm kind of reluctant to add another column to the pictures table then change some things to apply that.

I'm gonna fiddle around using AND instead of && and I'll let you know.


::EDIT::
Worked like a charm. It's so disheartening making those silly mistakes. thanks a bunch for pointing it out

and for anyone who is curious...the query that worked...

PHP Code:
"SELECT * FROM pictures WHERE AlbumID != '101' AND AlbumID != '102' ORDER BY RAND() LIMIT 1" 

Last edited by kbfirebreather; 07-30-2008 at 05:24 PM.. Reason: wasn't necessary for a brand new post.
kbfirebreather is offline
Reply With Quote
View Public Profile
 
Old 07-30-2008, 05:45 PM Re: random row from table with WHERE clause
Skilled Talker

Posts: 55
Name: Mike
Trades: 0
Quote:
Originally Posted by kbfirebreather View Post
well I was thinking about that, but what I have is an albums table with and activeAlbum column in it, which I planned on using. Find the inactive album, get their id's, etc.

so I'm kind of reluctant to add another column to the pictures table then change some things to apply that.

I'm gonna fiddle around using AND instead of && and I'll let you know.


::EDIT::
Worked like a charm. It's so disheartening making those silly mistakes. thanks a bunch for pointing it out

and for anyone who is curious...the query that worked...

PHP Code:
"SELECT * FROM pictures WHERE AlbumID != '101' AND AlbumID != '102' ORDER BY RAND() LIMIT 1" 
Heh, yeah I know what you mean. I make my fair share of obvious mistakes that I waste an hour trying to figure out what I did. Glad it worked for you.
XBSHX is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to random row from table with WHERE clause
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.47377 seconds with 12 queries