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']; ?>

1
  • What are the errors? Please edit and show them please :) Commented Sep 5, 2014 at 13:52

6 Answers 6

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'];
Sign up to request clarification or add additional context in comments.

4 Comments

yes...it's the data base name
@Xmayro yeah i thought so, so use mysqli instead, it should fit with the way you want, its not that hard to change your codebase as it stands now
This is the only correct answer on this, 4th param is not the db and either go with mysqli, PDO or use mysql_select_db().
actualy it worked.....i dont belive this....IT WORKED!!!!!!!!
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;

5 Comments

It will return 1, because the resultset only contains 1 row with value (example:) 1234
nope...but i'll try it too
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 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.
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.
-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

3 Comments

i'll also try this one
They don't say, a loop is faster, they say the method is faster. Without loop: $size = mysql_fetch_array();. Is faster then using while().... You only have 1 result, you don't need to loop..
Do you understand the error? Doesn't seem so
-1
$result = mysql_query("SELECT id FROM `wp_users`");
echo mysql_num_rows($result);

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

2 Comments

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.
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
-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'];
?>

1 Comment

it gives me the exacly same error.
-1

Try this

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

1 Comment

didn't work unfortunatly

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.