0

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?

1
  • Try a different variable name for your query. Commented Aug 14, 2015 at 10:26

1 Answer 1

1

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 :)

Sign up to request clarification or add additional context in comments.

4 Comments

The code does show it: $taskqu = mysql_query("SELECT COUNT(*), task_type FROM dotp_tasks WHERE task_type = '$key'");
THanx ive changed name of $taskqu here to different$taskqu = mysql_query("SELECT COUNT(*), task_type FROM dotp_tasks WHERE task_type = '$key'"); And it worked :)
Oh sorry! It's still early :) I somehow missed that. Let me edit that out of the question.
Updated the answer there so it should be more relevant. And... Glad to hear it worked! :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.