Easy PHP/MySQL Query question
January 7, 2007 8:59 PM
PHPfilter- I need to select several records from a MySQL db, based on their audto_increment id in the db. I know the numbers, but for some (stupid for sure) reason I can't get it to pull the records.
Here is my SQL:
SELECT posts.id, posts.user_id, posts.date_posted, posts.question, posts.summary, posts.type, posts.category FROM posts WHERE posts.is_flagged = 0 AND posts.active = 1 AND posts.id = 10,12,15,17,20 ORDER BY posts.id DESC
I think the hangup is with the group of id's at the end- I know that a query this way in ASP will work- but if this is the problem, how do I ask for this query of MySQL in PHP?
Here is my SQL:
SELECT posts.id, posts.user_id, posts.date_posted, posts.question, posts.summary, posts.type, posts.category FROM posts WHERE posts.is_flagged = 0 AND posts.active = 1 AND posts.id = 10,12,15,17,20 ORDER BY posts.id DESC
I think the hangup is with the group of id's at the end- I know that a query this way in ASP will work- but if this is the problem, how do I ask for this query of MySQL in PHP?
try "...posts.id IN (10,12,15,17,20)..."
Sorry if this is not right. I've never used MySQL. Fairly certain this would work in TSQL, though.
posted by trip and a half at 9:06 PM on January 7, 2007
Sorry if this is not right. I've never used MySQL. Fairly certain this would work in TSQL, though.
posted by trip and a half at 9:06 PM on January 7, 2007
SELECT `id`, `user_id`, `date_posted`, `question`, `summary`, `type`, `category` FROM posts WHERE `is_flagged` = 0 AND `active` = 1 AND `id` IN (10,12,15,17,20) ORDER BY `id` DESC
try that.
posted by cheaily at 9:10 PM on January 7, 2007
try that.
posted by cheaily at 9:10 PM on January 7, 2007
You don't need to put the "posts." in each column unless you're doing a select across multiple tables.
posted by cillit bang at 12:39 AM on January 8, 2007
posted by cillit bang at 12:39 AM on January 8, 2007
You don't need to put the "posts." in each column unless you're doing a select across multiple tables.
Although it's a good habit to get into and makes queries easy to change in the future.
posted by Blazecock Pileon at 4:33 AM on January 8, 2007
Although it's a good habit to get into and makes queries easy to change in the future.
posted by Blazecock Pileon at 4:33 AM on January 8, 2007
I use mysql on a daily basis and as long as your other conditions are met then saying id IN (10,12,15,17,20) will definitely work.
posted by mickbw at 9:11 PM on January 8, 2007
posted by mickbw at 9:11 PM on January 8, 2007
This thread is closed to new comments.
posted by nicwolff at 9:05 PM on January 7, 2007