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
[syntax=php]function user_count() {
return msql_result(msql_query("SELECT COUNT `user_id` FROM `users`"));
}

.....

There are <?php echo "$user_count()"; ?>[/syntax]

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.
[syntax=php] return mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users`"));[/syntax]

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.
[syntax=php]
<?php echo user_count() ; ?>
[/syntax]
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
[syntax=php]
$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>
[/syntax]

but I wanted to know if there was another way to do it

Using this method
[syntax=php]
function user_count() {
return mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users`"));
}
....
.....
......
There are <?php echo "$user_count"; ?>
[/syntax]

it doesn't work and also your suggestion didn't work either
but if I do it like this

[syntax=php]
//$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"; ?>
[/syntax]
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:
[syntax=php]return mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users`"), 0);[/syntax]

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.

[syntax=php]function user_count() {
return mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users`"), 0);
}

$user_count = $count;
$suffix = ($user_count != 1) ? 's' : '';
?>[/syntax]
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.¨

[syntax=php]<p>There are currently <strong><?php echo "$count"; ?></strong> registered user<?php echo $suffix; ?>.</p>

There are <?php echo "$user_count"; ?>
[/syntax]
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?
[syntax=php]
$user_count = user_count();[/syntax]
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();

[syntax=php]$user_count = user_count();
$user_count = htmlentities($user_count);[/syntax]

if that doesn't work, try adding the extra parameters.

[syntax=php]$user_count = user_count();
$user_count = htmlentities($user_count, ENT_COMPAT, 'UTF-8');[/syntax]
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