SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to count table record

    Hey.guys! I have a table here and would like to display all records.How could I count the number of record in table without manually specifying it in query statement.

    Code:
     $result =mysql_query(sprintf('SELECT name,classification,screeningTime 
                 FROM %s LIMIT 7', $tbl_name));

  2. #2
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried the following script,it can't work
    Code:
    $tbl_name = "movie";
    $query = mysql_query("SELECT * FROM $tbl_name");
    $num_row = mysql_num_rows($query);
    $result = mysql_query(sprintf('SELECT name,classification,screeningTime 
                 FROM %s LIMIT $num_row', $tbl_name)) or die('Cannot execute query.');

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,263
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by devil_vin View Post
    How could I count the number of record in table
    Code:
    select count(*) as number_of_rows_in_table from daTable
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't perform query in the script,what went wrong on it?

    Code:
    $tbl_name = "movie";
    $num_row = mysql_result(mysql_query("SELECT COUNT(DISTINCT name)FROM $tbl_name"),0);
    $result = mysql_query(sprintf('SELECT name,classification,screeningTime FROM %s LIMIT "$num_row"', $tbl_name)) or die('Cannot execute query.');

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,263
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    no idea

    moving thread to php forum
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    If you want to display all the records, there is no need to count them first and then use LIMIT.

    Just select them all:

    PHP Code:
    $result mysql_query(sprintf('SELECT name,classification,screeningTime FROM %s'$tbl_name)) or die('Cannot execute query.'); 


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
  •