0

My echo returns Array instead of value.

$postscount = mysql_query('select count(authorid) from topics where authorid="'.$author.'" ');
$posts = mysql_fetch_row($postscount);

Here's echo:

{
echo "Posts: ".$posts." ";
}

I tried with this:

{
    echo '<pre>';
    print_r($posts);
    echo '</pre>';
}

with print_r it retuns this:

Array ( [0] => 73 )

and with var_dump()

array(1) { [0]=> string(2) "73" }

tsvmitev
  • 69
  • 9
  • 1
    tried with print_r and got what? – Ares Draguna Oct 31 '14 at 10:53
  • maybe you have an empty array, try with `var_dump()` – Ares Draguna Oct 31 '14 at 10:53
  • `My echo returns Array` and this is right. – u_mulder Oct 31 '14 at 10:53
  • 1
    You [shouldn't use mysql_* functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use PDO or MySQLi. – Daniel Gelling Oct 31 '14 at 10:55

4 Answers4

0

Try with

echo $posts[0]['count(authorid)'];

Or just like

echo $posts[0];
GautamD31
  • 28,552
  • 10
  • 64
  • 85
0

Use this to print your complete array :

echo '<pre>'.var_export($posts, true).'</pre>';
David Ansermot
  • 6,052
  • 8
  • 47
  • 82
0

Try this code

$postscount = mysql_query('select count(authorid) from topics where authorid="'.$author.'" ') or die(mysql_error());
$posts = mysql_fetch_row($postscount);
var_dump($posts);
echo "There are ".$posts[0]. " authors";

Here

or die(mysql_error()) 

will tell you that query was successful or not.

Umair Ayub
  • 19,358
  • 14
  • 72
  • 146
0

Use mysqli

$posts = $name = $mysqli->query('select count(authorid) as postcount from topics where authorid="'.$author.'" ')->fetch_object()->postcount;  
echo $posts;

See more on PHP.net http://php.net/manual/en/book.mysqli.php