简体   繁体   中英

Boolean given (Trying to display statistics about database table)

I am trying to display statistics of my database table. I have a column called topics and it contains different values and then a column called priority which contains fields with values between 1-10.

Now, what I would like to do is display statistics for this table in the following format.

xx of the priority "1" have the topic "xxxxxx" as topic. xx of the priority "1" have the topic "yyyyyy" as topic. xx of the priority "1" have the topic "zzzzzz" as topic. xx of the priority "2" have the topic "xxxxxx" as topic. xx of the priority "2" have the topic "yyyyyy" as topic. xx of the priority "2" have the topic "zzzzzz" as topic.

And so on.

I have the following code but it is returning a " Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given" error when I try to use it.

<?php
    $hostname = "localhost";
    $username = "root";
    $password = "";
    $database = "dbname";

    $connection = mysql_connect($hostname, $username, $password) OR die('Could not connect to MySQL: ' . mysql_error());
    mysql_select_db($database);

$sql = "
       SELECT priority, COUNT(priority) as nbr_of_priorities, topic, 
       FROM table_Name
       GROUP BY priority, topic
    ";

$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
         echo $row['nbr_of_priorities'].'of the priority'.$row['priority'];
         echo 'has'.$row['topic'].'as topic';
}
?>

Have you run your query against the DB?

I see an erroneous comma in your SQL, after "topic"... your query could be failing, then $results would be false (which you are not testing), then your while would fail with that error...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM