Fetching

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • java.inet@gmail.com

    Fetching

    how to fetch the last five records in mysql through php

  • lnsoso

    #2
    Re: Fetching

    On 5ÔÂ22ÈÕ, ÏÂÎç3ʱ42·Ö, java.i...@gmail .com wrote:
    how to fetch the last five records in mysql through php
    1. save all query results in array, and use array_slice() get last
    five records.
    [php]
    $a = range(0,100);
    var_dump( array_slice($a, -5));

    2. modify u SQL cmd "ORDER BY DESC limit 5".

    so sorry , i can't express myself very well in english.

    Comment

    • Geoff Berrow

      #3
      Re: Fetching

      Message-ID: <1179819759.465 987.216870@36g2 000prm.googlegr oups.comfrom
      java.inet@gmail .com contained the following:
      >how to fetch the last five records in mysql through php
      Define 'last'.


      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • purcaholic

        #4
        Re: Fetching

        On 22 Mai, 09:42, java.i...@gmail .com wrote:
        how to fetch the last five records in mysql through php
        You can use mysql_data_seek to move the internal row pointer to the
        desired position. After then using a mysql_fetch_*() function in a
        while loop will iterate until the last position of the results.
        Example:
        [snip]
        if (!$result = mysql_query('SE LECT * WHERE 1=1')) {
        die('Invalid query: ' . mysql_error());
        }

        if ($num_rows = mysql_num_rows( $result)) {
        mysql_data_seek ($result, $num_rows-5);
        while ($row = mysql_fetch_arr ay($result)) {
        $data[] = $row;
        }
        }
        var_dump($data) ;
        [/snap]

        purcaholic

        Comment

        • gosha bine

          #5
          Re: Fetching

          On 22.05.2007 09:42 java.inet@gmail .com wrote:
          how to fetch the last five records in mysql through php
          >
          The words "first" and "last" only make sense when you tell mysql how to
          sort the records, and if you did, just replace ASC with DESC and you're
          in business ;)


          --
          gosha bine

          extended php parser ~ http://code.google.com/p/pihipi
          blok ~ http://www.tagarga.com/blok

          Comment

          • lnsoso

            #6
            Re: Fetching

            On 5ÔÂ22ÈÕ, ÏÂÎç3ʱ42·Ö, java.i...@gmail .com wrote:
            how to fetch the last five records in mysql through php
            1. save all query results in array, and use array_slice() get last
            five records.
            [php]
            $a = range(0,100);
            var_dump( array_slice($a, -5));

            2. modify u SQL cmd "ORDER BY DESC limit 5".

            so sorry , i can't express myself very well in english.

            Comment

            • Toby A Inkster

              #7
              Re: Fetching

              java.inet wrote:
              how to fetch the last five records in mysql through php
              Don't. Modify your SQL to only select the last five records.

              SELECT x.*
              FROM (SELECT *
              FROM table
              ORDER BY sortorder DESC
              LIMIT 5) x
              ORDER BY x.sortorder

              --
              Toby A Inkster BSc (Hons) ARCS
              Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

              Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

              Comment

              • ELINTPimp

                #8
                Re: Fetching

                On May 22, 4:04 am, Toby A Inkster <usenet200...@t obyinkster.co.u k>
                wrote:
                java.inet wrote:
                how to fetch the last five records in mysql through php
                >
                Don't. Modify your SQL to only select the last five records.
                >
                SELECT x.*
                FROM (SELECT *
                FROM table
                ORDER BY sortorder DESC
                LIMIT 5) x
                ORDER BY x.sortorder
                >
                --
                Toby A Inkster BSc (Hons) ARCShttp://tobyinkster.co. uk/
                Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
                completely agree with Toby. I used the exact method when designing a
                module that displayed a summary of the 5 most recent help desk tickets
                on our network management home page. always try to query only what
                you need from a database, anything more is just unnecessary overhead.

                Comment

                • ELINTPimp

                  #9
                  Re: Fetching

                  On May 22, 4:04 am, Toby A Inkster <usenet200...@t obyinkster.co.u k>
                  wrote:
                  java.inet wrote:
                  how to fetch the last five records in mysql through php
                  >
                  Don't. Modify your SQL to only select the last five records.
                  >
                  SELECT x.*
                  FROM (SELECT *
                  FROM table
                  ORDER BY sortorder DESC
                  LIMIT 5) x
                  ORDER BY x.sortorder
                  >
                  --
                  Toby A Inkster BSc (Hons) ARCShttp://tobyinkster.co. uk/
                  Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
                  completely agree with Toby. I used the exact method when designing a
                  module that displayed a summary of the 5 most recent help desk tickets
                  on our network management home page. always try to query only what
                  you need from a database, anything more is just unnecessary overhead.

                  Comment

                  Working...