SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could someone help me out please...

    $sql->Query("Select First, Last from table order by Last");
    for ($i = 0; $i < $sql->rows; $i++) {
    $sql->Fetch($i);
    If the above returns all rows and below returns the last five out of 10 rows...how would I return only the FIRST 5?

    $sql->Query("Select First, Last from table order by Last");
    for ($i = 5; $i < $sql->rows; $i++) {
    $sql->Fetch($i);
    I'm just not getting it I guess...
    Thanks

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What library are using for your db stuff?
    This should be a good workaround for you:

    PHP Code:
    $sql->Query("Select First, Last from table order by Last"); 
    for (
    $i 0$i 5$i++) { 
    $sql->Fetch($i); 
    Although it may not be the most effecient way to handle it, like if you are only wanting to select 5 reocrds from the db you could make your query look like:

    PHP Code:
    $sql->Query("Select First, Last from table order by Last LIMIT 5"); 
    for (
    $i 0$i $sql->rows$i++) { 
    $sql->Fetch($i); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow freddy, thanks!
    I'm just trying to learn by trial - without having to ask too many questions
    What I was trying to do was seperate the 10 rows into two pages of 5 ordered by last names so your first solution worked perfectly. I thought I had it, but I guess I was in over my head...back to the manual...
    Thanks again!

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See though there is no reason to return all ten rows if all you want is 5, I would suggest having a look at LIMIT in MySQL.

    You can say

    This will return the first five records
    select * from tablename LIMIT 5

    This will return the last five records
    select * from tablename LIMIT 5, 5
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    freedies second code is safer because you have to check how many rows there, what happens in your code if there is only 3 records?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •