[SOLVED] Fetch Array problem

This is a discussion on "[SOLVED] Fetch Array problem" within the PHP Forum section. This forum, and the thread "[SOLVED] Fetch Array problem are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Nov 30th, 2007, 23:00
Aso's Avatar
Aso Aso is online now
Chief Moderator

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 945
Blog Entries: 2
Thanks: 5
Thanked 18 Times in 16 Posts
Send a message via Skype™ to Aso
[SOLVED] Fetch Array problem

Hello!

I'm trying to show snippets of the most recent 3 posts in a database, which I'm having slight success with.

But my query displays the second most recent post followed by the third - the most recent is nowhere in sight! And even though my LIMIT is 3, it only ever shows 2 posts.

PHP: Select all

$data mysql_query("SELECT ID, 
                     post_date_gmt, 
                     post_content, 
                     post_title 
                     FROM wp_posts 
                     ORDER BY post_date_gmt DESC LIMIT 4"
)
                     or die(
mysql_error());
                     
$info mysql_fetch_array$data );

while(
$info mysql_fetch_array$data )) {
print 
'<p><b>';
$ptitle $info['post_title'];
$post_date $info['post_date_gmt'];
list(
$date$time) = explode(" "$post_date);
list(
$year$month$day) = explode("-"$date);
$new_date $day.'/'.$month.'/'.$year;
print 
$new_date'</b>';
Print 
' <a href="/news/?p='.$info['ID'].'" title="'.$ptitle.'">'.$ptitle.'</a><br />';
$post substr($info['post_content'], ,20);
Print 
$post."...</p>";

Any ideas?

Thanks

Alex
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)
Reply With Quote

  #2 (permalink)  
Old Dec 3rd, 2007, 16:49
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Fetch Array problem

Your problem is here

Code: Select all
$info = mysql_fetch_array( $data );

while($info = mysql_fetch_array( $data )) {
You only need to run

$info = mysql_fetch_array( $data );

inside the call to the while loop. Because the call to the mysql function changes the array pointer, it skips past the first record when you run it twice.

just use

while($info = mysql_fetch_array( $data )) {
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #3 (permalink)  
Old Dec 4th, 2007, 16:12
Aso's Avatar
Aso Aso is online now
Chief Moderator

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 945
Blog Entries: 2
Thanks: 5
Thanked 18 Times in 16 Posts
Send a message via Skype™ to Aso
Re: Fetch Array problem

Many thanks Rakuli
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)
Reply With Quote
  #4 (permalink)  
Old Dec 4th, 2007, 16:13
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: [SOLVED] Fetch Array problem

No problems
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
fetch the ENTIRE array? CloudedVision PHP Forum 3 Mar 5th, 2008 13:38
[SOLVED] Array code help longstand PHP Forum 3 Dec 1st, 2007 15:14
[SOLVED] Array sorting welshstew Classic ASP 6 Nov 28th, 2007 16:45
[SOLVED] array woes eon201 PHP Forum 1 Nov 6th, 2007 15:13
Array problem netwarriorgizmo JavaScript Forum 1 Jul 12th, 2004 21:25


All times are GMT. The time now is 22:22.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43