SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question problems with COUNT mysql function

    Im trying to get a count on how many rows i have in a table called Articles, at the momement there are no entries so i need the count function to return zero, however when i echo the results, it is blank.

    Here is my code for the mysql query:
    PHP Code:
    mysql_select_db("Articles"$con);
      
    $countR mysql_query('SELECT COUNT(*) FROM Articles');
      
    mysql_close($con); 
    Thanks everyone,
    Maxdream
    PHP is FUN

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Your first point of action when an echo does something unexpected is to use var_dump. You'll probably find that, if you're trying to echo $countR, it will say that the variable is actually a resource. Because it is.

    You need to get the result from the query before using it. The query returns a resultset just like any other query, but with only 1 row and 1 field.
    PHP Code:
    $countQuery mysql_query('SELECT COUNT(*) FROM Articles');
    $count mysql_result($countQuery0); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, i understand what your saying, that you need to optimize the result of the query before you use it, i tried your code and i ended up with and error though:
    Warning: mysql_result(): supplied argument is not a valid MySQL result resource,
    i take this to mean that its simply outputting a number rather than a MySQL type result, yet if that was true my orevious method would have worked...
    I think im confused with what exactly the COUNT function outputs and how to handle it.
    PHP is FUN

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,021
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by maxdream01 View Post
    I think im confused with what exactly the COUNT function outputs
    an integer

    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, but how can i access the integer, so that i can echo it, because i couldn't just save the query as a function and echo that...
    PHP is FUN

  6. #6
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks everyone but ifixed the problem i ended up outputting it like this:

    PHP Code:
    $query "SELECT id, COUNT(id) FROM Articles"
         
    $result mysql_query($query) or die(mysql_error());

    while(
    $count mysql_fetch_array($result)){
        echo 
    $count['COUNT(id)'];

    bit the real problem was that i misspelled my selected database, oops
    PHP is FUN


Tags for this Thread

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
  •