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
Display latest 5 mysql results
Old 06-22-2008, 12:52 PM Display latest 5 mysql results
Gilligan's Avatar
Website Designer

Posts: 1,670
Name: Stefan
Location: London, UK
Trades: 0
Using auto_increment in an' id' column in my mysql database, I want to display the 5 newest entries in this table

PHP Code:
$select mysql_query("SELECT * FROM recent_updates ORDER BY id DESC LIMIT 0,5");
$row mysql_fetch_array($select);

        echo 
'<p>' $row['date'] . '</p>
         <p>' 
$row['update_text'] . '</p>'
That only shows the newest one result, even though I've set the limit correct (i think)

Can someone help me?
__________________

Please login or register to view this content. Registration is FREE
Gilligan is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-22-2008, 02:49 PM Re: Display latest 5 mysql results
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
you mixed the arguments.
The full syntax (that i prefer because less error prone) is:
Code:
select * from recent_updates 
order by id desc
limit 5 offset 0
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-22-2008, 02:55 PM Re: Display latest 5 mysql results
Gilligan's Avatar
Website Designer

Posts: 1,670
Name: Stefan
Location: London, UK
Trades: 0
its still just displaying the one
__________________

Please login or register to view this content. Registration is FREE
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 06-22-2008, 04:58 PM Re: Display latest 5 mysql results
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
try just having:

PHP Code:
$select mysql_query("SELECT * FROM recent_updates ORDER BY id DESC LIMIT 5");
$row mysql_fetch_array($select);
 
        echo 
'<p>' $row['date'] . '</p>
         <p>' 
$row['update_text'] . '</p>'
TP apprieciated if it works
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 06-22-2008, 05:23 PM Re: Display latest 5 mysql results
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Ok, I see why, you don't loop over the result set from the db:
PHP Code:
$q="SELECT * FROM recent_updates ORDER BY id DESC LIMIT 5 OFFSET 0";
$r=mysql_query($q);
while(
$o=mysql_fetch_object($r)){
  echo<<<HTML
<p>Date: {$o->date}</p>
<p>text: 
{$o->update_text}</p>
<hr/>
HTML;

And that way, you should have every rows displayed
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-22-2008, 06:39 PM Re: Display latest 5 mysql results
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
It would be prudent to include a check to make sure that the query worked. I could have sworn I answered this question recently.

EDIT: I did at http://www.webmaster-talk.com/php-fo...t-updates.html


Is this a mistaken double post?
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE

Last edited by JeremyMiller; 06-22-2008 at 06:40 PM..
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 06-22-2008, 06:57 PM Re: Display latest 5 mysql results
Gilligan's Avatar
Website Designer

Posts: 1,670
Name: Stefan
Location: London, UK
Trades: 0
I didn't know what MySQLi was, and so left it as I'm working with a deadline. I realise now you have replied.

i took your Mysql query and used it in the more basic code, but forgot to loop over the results (thanks tripy)

TP for both of you (and I'll through one in for dan for trying )

EDIT: How would I make it so you can edit the date and text of it? Thanks
__________________

Please login or register to view this content. Registration is FREE

Last edited by Gilligan; 06-22-2008 at 06:59 PM..
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 06-22-2008, 07:21 PM Re: Display latest 5 mysql results
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Oh. Next time just ask and I can convert it for you. I always start with what I consider the best and then take it from there. Here is the adjusted code:

PHP Code:
<?php
$recent_updates 
'';
if (
$my_query mysql_query("SELECT `date`, update_text FROM recent_updates ORDER BY id DESC LIMIT 5")) {
  if (
mysql_num_rows($my_query) >0) {
    while (
$a_result mysql_fetch_object($my_query)) {
    
$recent_updates .= '
<p>'
.date('m/d/y',strtotime($a_result->date)).'</p>
<p>'
.htmlentities($a_result->update_text).'</p>';
    }
  }
}
if (
strlen($recent_updates) > 0) {
  echo 
'<div class="recent_updates">'.$recent_updates.'</div>';
}
?>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 06-22-2008, 07:34 PM Re: Display latest 5 mysql results
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Saw your edit. It's a bit much to come to a forum and ask people to write a whole script for you, but since you've been nice, I'll wave my usual resistance to this.

Here you go:

PHP Code:
<?php
if (isset($_POST['recent_update'])) {
  
$error_message '';
  foreach (
$_POST['recent_update'] as $id=>$fields) {
    
$id = (int)$id;
    if (
$id 0) {      
      
$date date('Y-m-d',strtotime($fields['date']));
      
$update_text mysql_real_escape_string(stripslashes($fields['update_text'])); //Can remove stripslashes if your server is configured properly.
      
if (!mysql_query("UPDATE recent_updates SET date='".$date."', update_text='".$update_text."' WHERE id=".$id." LIMIT 1")) {
        
$error_message .= 'There was an error updating record '.$id.'.  MySQL reported '.mysql_error().'<br />';
      }
    }
  }
  if (
strlen($error_message) > 0) {
    echo 
'<div class="error_message">'.$error_message.'</div>';
  }
}
$recent_updates '';
if (
$my_query mysql_query("SELECT id, `date`, update_text FROM recent_updates ORDER BY id DESC LIMIT 5")) {
  if (
mysql_num_rows($my_query) >0) {
    while (
$a_result mysql_fetch_object($my_query)) {
    
$recent_updates .= '
<p>'
.date('m/d/y',strtotime($a_result->date)).'</p>
<p>'
.htmlentities($a_result->update_text).'</p>
<div onClick="document.getElementById(\'recent_update_'
.$a_result->id.'\').style.display=\'block\';">Edit</div>
<div id="recent_update_'
.$a_result->id.'" style="display:none;">
<input type="text" size="10" name="recent_update['
.$a_result->id.'][date]" value="'.date('m/d/y',strtotime($a_result->date)).'" />
<textarea name="recent_update['
.$a_result->id.'][update_text]" rows="8" cols="80">'.htmlentities($a_result->update_text).'</textarea>
</div>'
;
    }
  }
}
if (
strlen($recent_updates) > 0) {
  echo 
'<form method="post"><div class="recent_updates">'.$recent_updates.'</div><input type="submit" value="Save Changes" /></form>';
}
?>
EDIT: I'll take a cut of your payment when you get it. j/k of course.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 06-22-2008, 09:45 PM Re: Display latest 5 mysql results
dansgalaxy's Avatar
Defies a Status

Posts: 6,522
Name: Dan
Location: Swindon
Trades: 0
Wow im slow, i missed the whole loop thing

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 06-23-2008, 04:20 AM Re: Display latest 5 mysql results
Gilligan's Avatar
Website Designer

Posts: 1,670
Name: Stefan
Location: London, UK
Trades: 0
Thank You!!
__________________

Please login or register to view this content. Registration is FREE
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 06-23-2008, 01:33 PM Re: Display latest 5 mysql results
Gilligan's Avatar
Website Designer

Posts: 1,670
Name: Stefan
Location: London, UK
Trades: 0
Sorry its displaying yyyy-mm-dd

Can it be dd/mm/yyyy please?
__________________

Please login or register to view this content. Registration is FREE
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 06-23-2008, 01:44 PM Re: Display latest 5 mysql results
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Not sure where you're talking about, but it sounds like you need to check out the date() syntax. It's at http://www.php.net/date . Check that out and let me know how your attempts go.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 06-23-2008, 02:24 PM Re: Display latest 5 mysql results
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
or rather than PHP date(), look for mysql DATE_FORMAT() to have the db return you the date directly in the right format
http://dev.mysql.com/doc/refman/5.0/...on_date-format
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-23-2008, 02:55 PM Re: Display latest 5 mysql results
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Here is the function I use to convert MySQL dates to any format I wish:
PHP Code:
function convert_date($date$format) {
$d strtotime($date);
$d date($format$d);
return 
$d;

Where $date is the MySQL formatted date, and $format the desired output. I have this saved as a code snippet because it comes up so often in my day to day work.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to Display latest 5 mysql results
 

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.39483 seconds with 12 queries