Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    PHP Code:
    <?php
    $query 
    "SELECT COUNT(*) FROM table WHERE column='value'";
    $result mysql_query($query);
    ?>
    With that code, how can I assign the number of rows returned?

    What I want to do is echo out the number of rows, that match a condition (the where clause I am fine with, it is assigning the number to a variable).

    Thanks in advance

  2.  

  3. #2
    Senior Member visualAd's Avatar
    Join Date
    Jan 2003
    Location
    Slough, UK
    Posts
    201
    Member #
    434
    You can five the column using an array index and assign it like this:
    PHP Code:
    <?php
    $query 
    "SELECT COUNT(*) FROM table WHERE column='value'";

    if (
    $result mysql_query($query)) {
      
    $row mysql_fetch_row($result);
      
    $count $row[0];
    }
    ?>

  4. #3
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Yes, this demonstrates the occasional usefulness of mysql_fetch_row vs mysql_fetch_assoc (which is still usable, but the actual column name in the returned recordset for a COUNT(*) command is weeeiiird). When you're doing a math operation like that, it's usually best to use mysql_fetch_row (or, use mysql_fetch_array to get both string and number indices if you need both).

  5. #4
    Junior Member
    Join Date
    Apr 2004
    Posts
    4
    Member #
    5666
    Why not assign the COUNT(*) column a virtual name?

    i.e

    PHP Code:
    <?php
    $query 
    "SELECT COUNT(*) AS cnt FROM table WHERE column='value'";

    if (
    $result mysql_query($query)) {
      
    $row mysql_fetch_row($resultMYSQL_ASSOC);
      
    $count $row['cnt'];
    }
    ?>
    Enables you to reference by name. Easy...


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:10 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com