1

Well...the question pretty much says everything, i have a database and i need to count how many rows it has(it represents the number of registered users) and i have to show it in html. it shows me this error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\LGCM\wp-content\themes\Avada-Child-Theme\home.php on line 381

i tried some stuff but i got a bunch of errors(i'm kind of new in this):

     <?php
     $db = mysql_connect('localhost','root','','lgcm_new');

     $result = mysql_query("SELECT COUNT(id) AS 'total' FROM wp_users");
     $row = mysql_fetch_assoc($result);
     $size = $row['total'];
     ?>

What am i doing wrong? i Just need to show how many user we have in our database, that's all i need.

The awnser was to use the msqli library(Given by use Ghost): query("SELECT COUNT(id) AS total FROM wp_users"); $result = $query->fetch_assoc(); echo $result['total']; ?>

Xmayro
  • 41
  • 1
  • 2
  • 11

6 Answers6

1

Use mysqli instead. Anyway,

Is lgcm_new a database name? In the manual, the fourth paramenter of mysql_connect is a mysql link, not the database name.

http://php.net/manual/en/function.mysql-connect.php

resource mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false [, int $client_flags = 0 ]]]]] )

$db = mysql_connect('localhost','root','','lgcm_new');
                                           // ^ this one

Use mysqli_connect instead. It should fit with the one you're using

$db = mysqli_connect('localhost','root','','lgcm_new');
$query = $db->query("SELECT COUNT(id) AS total FROM wp_users");
$result = $query->fetch_assoc();
echo $result['total'];
Kevin
  • 41,694
  • 12
  • 53
  • 70
0

Have you tried using the 'mysql_num_rows' feature?

//Count the number of rows from your result..
$Count = mysql_num_rows($result);

//Echo out the count
echo $Count;
Alex
  • 1,208
  • 16
  • 26
  • It will return 1, because the resultset only contains 1 row with value (example:) 1234 – Daniel W. Sep 05 '14 at 13:56
  • nope...but i'll try it too – Xmayro Sep 05 '14 at 13:57
  • In that case, Maybe change your sql query? Then use this piece of code I've provided to count them, you won't need the 'COUNT' part in your query, just use SELECT * FROM wp_users ? – Alex Sep 05 '14 at 13:59
  • @Alex Imagine you have 10000000000 rows in the table, like 2 GB of data. Using your solution, you will send ALL of it through the network. Using native mysql COUNT() only sends a few bytes. – Daniel W. Sep 05 '14 at 14:04
  • I highly doubt he will have that many rows in his table, it doesn't seem like he is using it for any type of highly professional standard business with mass data, mysql_num_rows works just fine for small databases, Besides he could just select what rows he wants to be selected using the WHERE clause etc. I do see your point though, but for this, I don't think it's really that important, as for a start he's using mysql_connect, any professional would not use that. – Alex Sep 05 '14 at 14:14
-1

Do something like this

while($row = mysql_fetch_array($result)){
 $size = $row['total'];
}

Use mysql_fetch_array very useful: mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_object

Community
  • 1
  • 1
Punitha Subramani
  • 1,467
  • 1
  • 8
  • 6
-1
$result = mysql_query("SELECT id FROM `wp_users`");
echo mysql_num_rows($result);

http://php.net/manual/en/function.mysql-num-rows.php

  • Imagine you have 10000000000 rows in the table. Using your solution, you will send ALL of them through the network. Using native mysql COUNT() only sends a few bytes. – Daniel W. Sep 05 '14 at 14:04
  • thats why i not starred the column, instead of it i only used `id`. other fact maybe is that i don't think that he'll have that lots of users in his wordpress database – Littlericket Sep 05 '14 at 14:08
-1

I guess you are using new version of PHP 5.4 series. This version does not support old style mysql functions. Instead try to use mysqli library. I guess it may work. I am not sure because i don't know which error you see.

<?php
 $db = mysqli_connect('localhost','root','','lgcm_new');

 $result = mysqli_query("SELECT COUNT(id) AS 'total' FROM wp_users");
 $row = mysqli_fetch_assoc($result);
 $size = $row['total'];
?>
Atul Jindal
  • 946
  • 8
  • 8
-1

Try this

$size = $row[0]['total'];