Finding out how mnay records are in a queryy

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

    #1

    Finding out how mnay records are in a queryy

    Hi,
    Write now I have code to read in a set of data from a surrey. Could I
    find out how many rows of data the query has, without adding to a
    counter each time I read a row?

    Also is there a way to start reading the database from a specific row,
    instead of always starting at the begging?

    This is what my code looks like


    $sql="SELECT * FROM zen_products where 1";

    if (!$results=mysq l_query($sql,$d blink))
    die("<brcould not query database");


    // can we figure out the size
    // go to a spefic loction without looping?
    while( $row=mysql_fetc h_array($result s))
    {
    extract($row);
    echo "=id";
    echo $products_id;
    }

    // close databse connection
    mysql_close($db link);

  • =?UTF-8?B?SXbDoW4gU8OhbmNoZXogT3J0ZWdh?=

    #2
    Re: Finding out how mnay records are in a queryy

    tedpottel@gmail .com wrote:
    Could I find out how many rows of data the query has, without adding to a
    counter each time I read a row?


    --
    ----------------------------------
    Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

    Now listening to: Deep Forest - Music.Detected_ (2002) - [2] Endangered
    Species (6:18) (98.333298%)

    Comment

    • C. (http://symcbean.blogspot.com/)

      #3
      Re: Finding out how mnay records are in a queryy

      On 3 Oct, 18:07, Iván Sánchez Ortega <ivansanchez-...@rroba-
      escomposlinux.-.punto.-.orgwrote:
      tedpot...@gmail .com wrote:
      Could I find out how many rows of data the query has, without adding toa
      counter each time I read a row?
      >

      >
      The mysql buffer lives outside the php memory limit - sometimes it may
      be a good idea to get a count of rows without reading them in
      So...

      depending on your DBMS version:

      $SQL="SELECT * FROM zen_products where 1"; // or whatever

      $prev_rows=chec k_cached_count( $SQL);
      if (!$prev_rows) {
      $rows="SELECT COUNT(*) FROM ($SQL)";
      $res=mysql_quer y($rows);
      $data=mysql_fet ch_row($res);
      $prev_rows=$dat a[0];
      save_cache_data ($SQL, $prev_rows);
      }
      ....

      - this will still hurt the disk I/O on the DBMS though - hence the
      caching.

      MySQL allows you to first fetch the number of rows then retrieve rows
      without hitting the datafiles again by using SQL_CALC_FOUND_ ROWS and
      FOUND_ROWS() RTFM for more details.

      C.

      Comment

      Working...