Little PHP help

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
I have this query

PHP:
<?php
$album_views = mysql_query("SELECT SUM(views) FROM `photo_albums`") or die(mysql_error());
?>
There have been <?php echo $album_views; ?> total album views.

and i am getting resource ID #21

basically i want the number of views for the photo albums but when i run that i get the error

but when i put that into phpmyadmin it works. anybody know whats wrong
 

Brandon

Former Senior Account Rep
Community Support
Messages
19,181
Reaction score
28
Points
48
Try this

PHP:
$album_views = mysql_query("SELECT SUM(`views`) FROM `photo_albums`") or die(mysql_error());

Ill look more into it tommarrow
</span></span>
 

Corey

I Break Things
Staff member
Messages
34,553
Reaction score
204
Points
63
Do print_r($album_views) and see what is says, usually a resource ID means it's in an array.

-Corey
 

acidburn0520

New Member
Messages
117
Reaction score
0
Points
0
Shouldn't you be using $album_views['views'] rather than just $album_views?
 

Torch

New Member
Messages
634
Reaction score
0
Points
0
Actually, mysql_query with SELECT statement returns a resource which isn't very useful to you untill you pass it to some function that converts it to data. In your case, the best one would be mysql_fetch_array. So what you need to do is:
PHP:
<?
$album_views = mysql_query("SELECT SUM(views) FROM `photo_albums`") or die(mysql_error());
$album_views = mysql_fetch_array($album_views); //Makes an array out of internal object/resource
?> 
There have been <?=$album_views[0]?> total album views.

There you go, it should work :)
 
Last edited:

acidburn0520

New Member
Messages
117
Reaction score
0
Points
0
Oh yeah... I'm so used to vBulletin -- it uses OOP, so everything is $db.
 

Origin

New Member
Messages
1,082
Reaction score
0
Points
0
Plus you need to set it as SUM(views) as sum_of_views instead.
 
Top