0

I tried to count using following- include 'db_login';

    $sql = "(SELECT  * , COUNT(*)  FROM  `table1` 
    WHERE  `ADVERTISERCATEGORY` LIKE  '%something%'
    GROUP BY `MANUFACTURER` ORDER BY COUNT DESC )";
    $row = mysql_fetch_array($sql);
    $total = $row[0];
    echo "Total rows: " . $total;

but i got following error message-

Warning: mysql_fetch_array() expects parameter 1 to be resource, 
string given in /home/content/43/10130843/html/fashion_test.php on line 169

i am learning php/mysql therefore i need help. thanks

sgress454
  • 24,870
  • 4
  • 74
  • 92

1 Answers1

1

You haven't called mysql_query(), so you can't fetch the array yet...

$sql = "SELECT  * , COUNT(*)  FROM  `table1` 
WHERE  `ADVERTISERCATEGORY` LIKE  '%something%'
GROUP BY `MANUFACTURER` ORDER BY COUNT(*) DESC ";
$query = mysql_query($sql) or die(mysql_error()); // exit on error
$row = mysql_fetch_array($query);

Note: mysql_query() and the rest of the mysql_* library have been deprecated for some time, you should make the move to paramterized queries with mysqli_* or PDO while you're in the development phase.

Community
  • 1
  • 1
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • now it says - Unknown column 'COUNT' in 'order clause' – user3453180 Mar 23 '14 at 20:24
  • thanks. i am one step ahead - but i am getting result as --Total rows: one record – user3453180 Mar 23 '14 at 20:35
  • Are you expecting to return one result or many? If you expect one result, your query will be returning multiple, and if you expect multiple results your PHP is only supporting the first result. You either need to rework your query or loop over the results. – scrowler Mar 23 '14 at 20:40
  • I tried many combinations, i think my query is returning one result from a column and no count. I am expecting many results. – user3453180 Mar 23 '14 at 21:08
  • 1
    I found it- `$sql = "SELECT * , COUNT(*) FROM `table1` as newcount` and then--- while ($row = mysql_fetch_array($sql)) { echo $row['name']; // echo $row['comment_count']; // Gives the row count for the associated paragraph } – user3453180 Mar 23 '14 at 21:39