0

i'm using the following to count the number of rows in my table:

$book_count = query("SELECT status FROM scifi_book WHERE read = $uid");
(count($book_count));
echo $book_count;

and i get the following error:

Notice:  Array to string conversion on line 167 

(which is the line echo $book_count;)

FIY, the query function is defined and works fine. Never had any issues to insert, select, update and delete.

Am I missing something here?

2
  • What does query() return? And where does $follower_count in your question come from? Commented Jul 5, 2013 at 18:18
  • @andrewsi this was a mistake when i copy pasted my code.Just edited it. Commented Jul 5, 2013 at 18:22

5 Answers 5

2

Try this:

$book_count = query("SELECT status FROM scifi_book WHERE read = $uid");
echo count($book_count);

Also, you need to use print_r($book_count) since your $book_count is not a string.

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

4 Comments

I don't understand the last statement (use print_r because $book_count is not a string). May you elaborate, please?
Ah, got it. He also echoes the variable content; didn't see that before.
@Racso: you use echo for strings, $book_count is an array object returning query results. To echo array use print_r
Thanks, works like a charm ! I'll be able to accept your answer in 6 minutes
2

Suggestion: if you only use that query for getting the count, this may be a bit better:

$book_count = query("SELECT count(*) FROM scifi_book WHERE read = $uid");

Comments

1

your query function seems to return an array, not a string. Instead of echo $follower_count use print_r($follower_count) to see what's inside the query response.

Comments

1

The reason you are seeing that error is because you are echoing Array which is the returned by your query function. echo construct only works on strings, please see the documentation here: http://www.php.net/manual/en/function.echo.php.

Had you used print_r or var_dump then you wouldn't have seen that error. So as @A.S. Roma and Nathaniel Granor suggest use print_r

Comments

1
$book_count = query("SELECT status FROM scifi_book WHERE read =".$uid);
(count($book_count));
echo $book_count;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.