简体   繁体   中英

Problems in printing array (error scalar value)

I can't find the way to print this array. This is my script:

foreach ($task_types as $key => $value) {
    echo $key;
    $taskqu = mysql_query("SELECT COUNT(*), task_type FROM dotp_tasks WHERE task_type = '$key'");
    while ($row = mysql_fetch_array($taskqu)) {
        $taskqu[$row['task_type']] = $row[0];
    }
}
echo "<pre>";
print_r($taskqu);
echo "</pre>";

And when I try to print it, here's the error I get:

Warning: Cannot use a scalar value as an array in

How can I solve this?

Dereferencing a scalar value with array syntax triggers that warning. For example:

$a = 1;
$a[] = 1;

Triggers:

PHP Warning: Cannot use a scalar value as an array in /Users/darragh/Sites/__.php on line 5

What's the initial value of $taskqu ? It's clearly not an array, therefore:

$taskqu[$row['task_type']] = $row[0];

is an invalid operation and raises this PHP warning.

This is because $taskqu is already initialised with the return value of mysql_query , which (because your query is a SELECT statement) is either a resource type or false .

Either way the value is initialised, is not an array and, as such, cannot be dereferenced with array syntax.

Hope this helps :)

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