SQL COUNT

Post here if you need help with SQL.
Post Reply
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

SQL COUNT

Post by Fidbeck »

Hello there
I have a problem here

let me try to explain
In my newsletter I want to show in the page how many people are registered

I tried
function user_count() {
    return msql_result(msql_query("SELECT COUNT `user_id` FROM `users`"));
}

.....

There are <?php echo "$user_count()"; ?>
In that tutorial it worked and what I get is only this
There are $user_count users
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: SQL COUNT

Post by Temor »

You shouldn't use msql for this.
use mysql instead.
 return mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users`"));
and do not use $ before user_count().
you should also not use quotes around it. quotes are meant for strings, not to echo variables or functions.
<?php echo user_count() ; ?>
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: SQL COUNT

Post by Fidbeck »

but i can put it like that right?

got this error
Warning: Wrong parameter count for mysql_result() in /home/a5510829/public_html/mailing_list/signup.php on line 34



Curious thing happened...
I already had the counting working this way
$query = mysql_query("SELECT COUNT('user_id') FROM `users`"); // this is the query
$query = mysql_query("SELECT * FROM `users`"); // You can do this too, Selecting everything from users * = everything
$count = mysql_num_rows($query); // Assuming if you are counting users or something, mysql_num_rows() is the right function for it....
....
.....
......
<p>There are currently <strong><?php echo "$count"; ?></strong> registered user<?php echo $suffix; ?>.</p>
but I wanted to know if there was another way to do it

Using this method
function user_count() {
    return mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users`"));
}
....
.....
......
There are <?php echo "$user_count"; ?>
it doesn't work and also your suggestion didn't work either
but if I do it like this
//$query = mysql_query("SELECT COUNT('user_id') FROM `users`"); // this is the query
$query = mysql_query("SELECT * FROM `users`"); // You can do this too, Selecting everything from users * = everything
$count = mysql_num_rows($query); // Assuming if you are counting users or something, mysql_num_rows() is the right function for it....

function user_count() {
    return mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users`"));
}
....
.....
......
<p>There are currently <strong><?php echo "$count"; ?></strong> registered user<?php echo $suffix; ?>.</p>

There are <?php echo "$user_count"; ?>
they both show the same number of registered users.
Now I don't know why adding
$query = mysql_query("SELECT * FROM `users`");
$count = mysql_num_rows($query);
made thee two work :S
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: SQL COUNT

Post by Temor »

try adding , 0 at the end, like this:
return mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users`"), 0);
does it work?
it should.
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: SQL COUNT

Post by Fidbeck »

I doesn't but really don't know why.
I'm gonna send you the code.

Thanks
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: SQL COUNT

Post by Temor »

You need to rework the last few lines.
function user_count() {
   return mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users`"), 0);
}
 
$user_count = $count;
$suffix = ($user_count != 1) ? 's' : '';
?>
You need to move the last two lines outside of the file, and also rewrite them a bit.
$count has no value, so $user_count will not have a value either.¨
<p>There are currently <strong><?php echo "$count"; ?></strong> registered user<?php echo $suffix; ?>.</p>
 
There are <?php echo "$user_count"; ?>
 
where does $count get it's value from? The same question goes for $user_count.
remove the quotes around them, otherwise php will interpret it as a string and actually print $user_count instead of its value.
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: SQL COUNT

Post by Fidbeck »

in my last message I said I doesn't, sorry for the typo, it was supposed to be It doesn't

Still not working... :S
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: SQL COUNT

Post by Temor »

have you given $user_count a value?
$user_count = user_count();
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: SQL COUNT

Post by Fidbeck »

No I haven't, it's a much simpler way and its working.

Thanks

I'm getting this stupid symbol but i'll find a way to take it off :)
� There are 0 registered users
this is the charset
<meta charset="utf-8">
thanks
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: SQL COUNT

Post by Temor »

Sometimes the charset gets messed up even though you set it. Try running it through htmlentities();
$user_count = user_count();
$user_count = htmlentities($user_count);
if that doesn't work, try adding the extra parameters.
$user_count = user_count();
$user_count = htmlentities($user_count, ENT_COMPAT, 'UTF-8');
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: SQL COUNT

Post by Fidbeck »

I didn't even had to use that thing lol
What I did was just... use the return button, I think that's the name (the one above the enter)
stupid thing I know.
I'm going to use the 1st suggestion, htmlentities, there's no problem in adding a bit more security to it :)

Thanks
Post Reply