0

I am trying to get the count of users on my site associated with a certain company, but something is wrong with my query. I keep getting 'no result' or a result of array:

$coresults = mysql_query("SELECT COUNT(user_id) FROM ".DB_USERS." WHERE user_company=".$jdata['job_company']."");
$count = mysql_fetch_array($coresults);

I have also tried with PDO with no success

$nRows = $pdo->query('select count(*) from blah')->fetchColumn(); 
echo $nRows; 
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Roots_Dev
  • 9
  • 2
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Sep 28 '15 at 15:33
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Sep 28 '15 at 15:33
  • 2
    Have you tried `var_dump($nRows);` / `var_dump($count);` to see what you're actually getting? Have you checked your error logs? Do you have any exception handling? etc etc – Jonnix Sep 28 '15 at 15:33

2 Answers2

0

mysql_fetch_array returns an array of your executed query. Use mysql_num_rows instead:

$coresults = mysql_query("SELECT user_id FROM ".DB_USERS." WHERE user_company=".$jdata['job_company']."");
$count = mysql_num_rows($coresults);
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Gautam
  • 1,046
  • 5
  • 20
  • Will probably error unless `user_company` is an INT, also, that just seems like unnecessary overhead when all they want is a `COUNT`. It doesn't really answer the question imo. – Jonnix Sep 28 '15 at 15:37
  • thanks @Jon for the correction. I thought that user was taking count with help of mysql_fetch_array function – Gautam Sep 28 '15 at 15:40
  • Thank you but im still getting a result of array. user_company is an INT btw sorry for not making that clear above. i did try mysql_fetch_array as well as mysql_num_rows as well as mysql_result – Roots_Dev Sep 28 '15 at 16:31
0

You can solve this by giving a name to your count field and using mysql_fetch_assoc function:

$coresults = mysql_query("SELECT COUNT(user_id) AS usercount FROM ".DB_USERS." WHERE user_company=".$jdata['job_company']."");
$count = mysql_fetch_assoc($coresults);

And then, you access the field like this:

print ($count['usercount']);

The mysql_fetch_assoc function turns the selected fields into array keys, then you can access them like a simple array.

Fabiano
  • 5,001
  • 2
  • 28
  • 31
  • I had tried this earlier this no luck I tried your example again and i am returning array as my result. – Roots_Dev Sep 28 '15 at 16:30
  • And what appears if you execute var_dump($count['usercount'])? It may show the value you want. If it is returning an array, then the result probably will appear in the first cell. Try it and show us what you got. – Fabiano Sep 28 '15 at 18:07
  • I get 3 lines of null for the 3 users i have if they have no value in job_company. when i put a value into one of the account i get a return of array – Roots_Dev Sep 28 '15 at 21:20