How to fetch data from MySQL using PHP in DESC order?
Posted: Thu Aug 16, 2012 10:50 am
I have a table in MySQL database named "posts".
The data of "posts" is as follow.
id sender_id post_type text date
1 2 t hi Saturday, 4th August 2012, 7:43:18 pm
3 1 t hello Tuesday, 7th August 2012, 4:39:31 am
4 1 t how r u? Tuesday, 7th August 2012, 4:39:39 am
5 1 t thats cool Tuesday, 7th August 2012, 7:49:35 am
6 1 t good Tuesday, 7th August 2012, 7:49:45 am
7 2 t fine Tuesday, 7th August 2012, 7:50:05 am
8 2 t great Tuesday, 7th August 2012, 7:50:08 am
There is another table named "subscription".
The data of "subcription" is as follow.
id subscriber subscribed date
1 3 1 Friday, 6th July 2012, 7:25:41 pm
2 4 1 Saturday, 7th July 2012, 4:38:26 pm
3 4 2 Sunday, 8th July 2012, 6:46:52 pm
4 1 2 Monday, 9th July 2012, 9:48:32 pm
5 1 3 Tuesday, 10th July 2012, 3:44:18 pm
Now I want to fetch data from "posts" in DESC order by id of "posts" on a page current URL id is subscriber
of that id who posted.
I am using this code for this.
This is giving me all posts of subscribed user
but it gives one user post first and then second user and then so on...
Please tell me how can I fetch posts by its id DESC order.
Thanks.
The data of "posts" is as follow.
id sender_id post_type text date
1 2 t hi Saturday, 4th August 2012, 7:43:18 pm
3 1 t hello Tuesday, 7th August 2012, 4:39:31 am
4 1 t how r u? Tuesday, 7th August 2012, 4:39:39 am
5 1 t thats cool Tuesday, 7th August 2012, 7:49:35 am
6 1 t good Tuesday, 7th August 2012, 7:49:45 am
7 2 t fine Tuesday, 7th August 2012, 7:50:05 am
8 2 t great Tuesday, 7th August 2012, 7:50:08 am
There is another table named "subscription".
The data of "subcription" is as follow.
id subscriber subscribed date
1 3 1 Friday, 6th July 2012, 7:25:41 pm
2 4 1 Saturday, 7th July 2012, 4:38:26 pm
3 4 2 Sunday, 8th July 2012, 6:46:52 pm
4 1 2 Monday, 9th July 2012, 9:48:32 pm
5 1 3 Tuesday, 10th July 2012, 3:44:18 pm
Now I want to fetch data from "posts" in DESC order by id of "posts" on a page current URL id is subscriber
of that id who posted.
I am using this code for this.
Code: Select all
$sub_query =
mysql_query(" SELECT subscribed FROM subscription
WHERE subscriber = $uid ");
while($sub_row = mysql_fetch_assoc($sub_query))
{
$subscribed = $sub_row['subscribed'];
$post_query =
mysql_query(" SELECT * FROM posts
WHERE sender_id = $subscribed ORDER by id DESC");
while($post_row = mysql_fetch_assoc($post_query))
{
echo $post_id = $post_row['id'].'
';
echo $sender_id = $post_row['sender_id'].'
';
echo $post_text = $post_row['text'].'
';
}
}This is giving me all posts of subscribed user
but it gives one user post first and then second user and then so on...
Please tell me how can I fetch posts by its id DESC order.
Thanks.