-2

I'm trying to display the count of a database table on my website. The code I'm using is:

<?php 
    $results = mysql_query("SELECT COUNT('id') from `idols`");
    $count = mysql_result($results, 0);
    $idols = number_format($count); 
    echo "$idols";
?>

But when I test the code it gives me this warning:

Warning: mysql_result() expects parameter 1 to be resource, boolean given on line 92.

Can anyone help me with it?

Tim
  • 8,227
  • 30
  • 100
  • 173
  • You're never checking the return value of `mysql_query` to see if there was an error. – lc. Mar 18 '13 at 00:19
  • See if this works [MYSQL CONNECT + COUNT() QUERY][1] [1]: http://stackoverflow.com/questions/607264/how-can-i-count-the-numbers-of-rows-that-a-mysql-query-returned – Grmn Mar 18 '13 at 00:21
  • Try to call [mysql_error](http://php.net/manual/en/function.mysql-error.php) after `mysql_query` – Iswanto San Mar 18 '13 at 00:24
  • [Google shows 1630 duplicates](https://www.google.ca/search?q=site:stackoverflow.com+%22mysql_result()+expects+parameter+1+to+be+resource%2C+boolean%22&oq=site:stackoverflow.com+%22mysql_result()+expects+parameter+1+to+be+resource%2C+boolean%22). – user229044 Mar 20 '13 at 03:45

2 Answers2

0

The problem you're encountering is that PHP returns FALSE for some objects if it encounters an error. First print or print_r the $results object, and only once you've determined its value proceed.

Use the syntax given in Example #1 here: http://php.net/manual/en/function.mysql-query.php

Also, mysql_query is deprecated and you probably shouldn't be using it in case your site eventually wants to upgrade to PHP 5.

0

Your query is returning a boolean false.

Put this after your query to see the error you're getting.

if (mysql_errno()) {
    printf(mysql_error());
}

Also, the regular mysql extension is pretty out of date, you might want to consider using PDO or at least Mysqli

castis
  • 8,041
  • 4
  • 41
  • 62