+ Reply to Thread
Results 1 to 3 of 3

Thread: COUNT(*), PHP and MySQL

  1. #1
    wjh2303 is offline x10 Sophmore wjh2303 is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    139

    COUNT(*), PHP and MySQL

    i wish to count the number of rows with a given content, group them by one of the columns and print the results:

    code:
    $countdet=mysql_query("SELECT size, COUNT(*) FROM Details WHERE mapLoc = '$rowloc[id]' GROUP BY size") or die(mysql_error());

    while($rowcd=mysql_fetch_array($countdet)){
    switch($rowcd[size]){
    case "GP":
    echo "<img src=\"GP.png\"> $rowcd['COUNT(*)'] Gigapixel images<br>";
    break;
    case "L":
    echo "<img src=\"L.png\"> $rowcd['COUNT(*)'] large images<br>";
    break;
    case "M":
    echo "<img src=\"M.png\"> $rowcd['COUNT(*)'] medium images<br>";
    break;
    case "S":
    echo "<img src=\"S.png\"> $rowcd['COUNT(*)'] small images";
    break;
    }
    }


    as above, i get the error:
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

    and if i change $rowcd['COUNT(*)'] to $rowcd[COUNT(*)] i get the error: unexpected ( was expecting ].

    any ideas? i just want it to return the value of the count... the mysql query works fine when i put it in, and the line number on the errors point to this bit of the code

    TIA

    edit: the example on this (http://www.tizag.com/mysqlTutorial/mysqlcount.php) is pretty much what i want to achieve, but am failing to do
    Last edited by wjh2303; 06-03-2009 at 06:47 AM.

  2. #2
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: COUNT(*), PHP and MySQL

    Array indexes cannot contain whitespace or asterisks. You can use mysql_fetch_array, which creates an array with both associative and numeric indexes, or you can alias the count() result:

    Code:
    SELECT size, COUNT(*) AS count FROM ....
    Now you can use $rowcd['count'] or "{$rowcd[count]}" I use {} around all PHP variables, so PHP automatically knows where the variable starts and where it ends
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  3. #3
    wjh2303 is offline x10 Sophmore wjh2303 is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    139

    Re: COUNT(*), PHP and MySQL

    thanks, that worked

+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. tons of PHP Resources
    By Chris S in forum Scripts & 3rd Party Apps
    Replies: 10
    Last Post: 01-16-2009, 10:07 AM
  3. Process PHP from a MySQL array
    By tnl2k7 in forum Programming Help
    Replies: 12
    Last Post: 05-05-2008, 10:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers