Page 1 of 1

SQL COUNT

Posted: Wed Oct 24, 2012 5:58 pm
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

Re: SQL COUNT

Posted: Wed Oct 24, 2012 8:06 pm
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]

Re: SQL COUNT

Posted: Wed Oct 24, 2012 8:09 pm
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

Re: SQL COUNT

Posted: Wed Oct 24, 2012 8:47 pm
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.

Re: SQL COUNT

Posted: Thu Oct 25, 2012 7:53 am
by Fidbeck
I doesn't but really don't know why.
I'm gonna send you the code.

Thanks

Re: SQL COUNT

Posted: Thu Oct 25, 2012 10:57 am
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.

Re: SQL COUNT

Posted: Thu Oct 25, 2012 11:45 am
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

Re: SQL COUNT

Posted: Thu Oct 25, 2012 1:22 pm
by Temor
have you given $user_count a value?
[syntax=php]
$user_count = user_count();[/syntax]

Re: SQL COUNT

Posted: Thu Oct 25, 2012 1:33 pm
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

Re: SQL COUNT

Posted: Thu Oct 25, 2012 1:43 pm
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]

Re: SQL COUNT

Posted: Thu Oct 25, 2012 2:32 pm
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