PHP计数行返回错误

PHP计数行返回错误

问题描述:

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?

我正在使用以下内容来计算表格中的行数: p>

  $ book_count = query(“SELECT status FROM scifi_book WHERE read = $ uid”); 
(count($ book_count)); 
echo $ book_count; 
  code>  pre> \  n 
 

我收到以下错误: p>

 注意:第167行的数组到字符串转换
  code>  pre> 
 
  

(行 echo $ book_count; code>) p>

FIY, query code>函数已定义并正常工作。 从来没有插入,选择,更新和删除任何问题。 p>

我在这里遗漏了什么吗? p> div>

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.

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.

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");

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

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