mysql query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

mysql query

Post by pedroz »

I have the following table:

posts | replies
1 | 5
2 | 2
3 | 0
4 | 5
5 | 3
6 | 4
7 | 8
8 | 3
9 | 0
10 | 1

I would like to order the last 5 posts order by replies DESC

posts| replies
7 | 8
6 | 4
8 | 3
10 | 1
9 | 0

Could you provide my the query?
...
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Wow, you're so close to being there.. what's stopping you from completing the query?

Anywho,

Code: Select all

SELECT posts FROM tablename ORDER BY replies DESC
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Post by pedroz »

Code: Select all

SELECT posts FROM tablename ORDER BY replies DESC
I think this code will display all posts and I need the last 5 five posts
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Oh, I'm sorry, I didn't notice that part. :|

Code: Select all

SELECT posts FROM tablename ORDER BY replies DESC LIMIT 0,5
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Post by pedroz »

It seems I have to execut a subquery but I am not receiving any echo... :(

What is wrong with the code above?

Code: Select all

$Table='daTable';
$discussions=5;
$fields='posts,replies';

$select="SELECT * FROM '.$Table.' ORDER BY post DESC LIMIT '.$discussions.'";

$cols=mysql_fetch_row(mysql_query('SELECT '.$fields.' FROM ('.$select.') ORDER BY replies DESC'));
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

i dont think that the () is needed around the vars maybe?
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Post by pedroz »

1. First part I found the error

Code: Select all

$select='SELECT * FROM '.$Table.' ORDER BY post DESC LIMIT '.$discussions.'';
instead

Code: Select all

$select="SELECT * FROM '.$Table.' ORDER BY post DESC LIMIT '.$discussions.'";

2. Second error

Code: Select all

$cols=mysql_fetch_row(mysql_query('SELECT '.$fields.' FROM ('.$select.') dt ORDER BY replies DESC'));

But still not running well the query. Only showing me one result instead 5


Removing () it will not show any result...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_fetch_row() returns one record. You'll need to call it in a loop to get all the rows.
Post Reply