tags:

views:

45

answers:

3

I'm trying to count how many times an article has been rated by my members buy using PHP to count a certain articles total entered ratings that have been stored in my MySQL database.

I really want to use PHP and not MySQL to do this and was wondering how I can do this?

I hope I explained this right?

An example would be very helpful my MySQL database that holds the ratings are listed below.

Here is the MySQL database.

CREATE TABLE articles_ratings (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
ratings_id INT UNSIGNED NOT NULL,
users_articles_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
date_created DATETIME NOT NULL,
PRIMARY KEY (id)
);


CREATE TABLE ratings (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
points FLOAT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);
+1  A: 

If that's really what you want, you could SELECT the ratings and then use http://php.net/manual/en/function.mysql-num-rows.php to count them. Is this what you had in mind?

Eric Mickelsen
But thats ridiculous. Why would someone load all the rows in memory and just take the count instead of doing count(*) in sql?
Hasan Khan
Because he really wants to, apparently. Obviously, it's quite inefficient, but if the number of ratings is very low, the difference won't be noticable. This person's whole persona is based on using php to count things - who am I to challenge someone's identity?
Eric Mickelsen
Ask the the question asker, but (s)he is really specific about this.
Veger
LOL, I didn't notice their name is CountPHP. "Job description: you will count things with PHP."
Kaleb Brasee
+3  A: 

It's much easier just to do it with SQL:

select count(*) from articles_ratings where id = (id value)

You could of course just select * from articles_ratings where id = (id value), then loop through all the rows to count them -- but if the database can do all this work for you, then it's usually best to use it!

Kaleb Brasee
The user said that (s)he **really wants to use PHP**, so your answer is unrelated/wrong.
Veger
I *did* say how it's possible with PHP -- select all the rows, then count them. But I'm not going to say it's a good idea, because it's not.
Kaleb Brasee
+1  A: 

"I really want to use PHP" - this will mean you will retrieve all rows from MySQL server and count them using PHP loop?

This is wrong - use SQL to aggregate information, then retrieve it from database.

noonex