Jump to content

Loops in php


essjay_d12

Recommended Posts

I want to pull out the latest 10 results from my database, but im just not sure, i was thinking like this...

loop (1 to 10) {

query = Select NAME,.,..,.,..,DATE FROm database ORDER BY DATE

ECHO RESULTS

}

But I think the above structure (obviousy not code) is still wrong, wont that above structure when implemented in php just pull out 10 of the same ones (i.e the latest one only) and not the latest 10!

PLease help

Thanks
Link to comment
Share on other sites

Just do your normall mysql query like so:
[code]<?php

//database connection code goes here

$sql = "SELECT name, email, date From users ORDER BY date";
$result = mysql_query($sql) or die("Error with query " . mysql_query());

while($row = mysql_fetch_array($result))
{
    echo $row['name'] . '<br>';
    echo $row['email'] . '<br>';
    echo $row['date'] . '<br>';
}

?>[/code]Ands that is all there is to getting data out of mysql and displaying it.
Link to comment
Share on other sites

If you just want the first ten rows ordered by date then append your query with LIMIT 1, 10 like so:
[code]$sql = "SELECT name, email, date From users ORDER BY date LIMIT 1, 10 ";[/code]
MySQL will query the database and order its results by the date and then it'll limit the already ordered rows by 10. So only 10 or less results (depending on how many rows there are in the database) should be returned.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.