...

View Full Version : Problem counting from table



StanLytle
08-08-2007, 07:02 PM
I'm stumped. I finally got rid of all the MySQL error messages, but the "if" part doesn't seem to work. I want to count the number of Active='0' photos a user has, and if more than ten, then exit the upload process with a message. My problem seems to be with " COUNT(UserID) AS QueueCount" and "if ($QueueCount > 10)". Any suggestions? Here's what I have:

if ($UserLevel == '4')
{
$query = "SELECT PhotoID, COUNT(UserID) AS QueueCount FROM Photos WHERE (Active = '0') AND (UserID = '$SessionUserID') GROUP BY UserID";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
if ($QueueCount > 10)
{
print "You have reached your upload limit.";
print "When your submissions have been processsed,";
print "you will be able to resume uploading";
require ("Footer.php");
exit;
}
}
}

Thanks,
Stan

whizard
08-08-2007, 07:10 PM
try

if($row['QueueCount'] > 10)

HTH
Dan

Fumigator
08-08-2007, 07:24 PM
BTW there's no need to select count() from the table-- you can use the mysql_num_rows() function to get the number of rows in any query result. And it is pointless to use GROUP BY on a column that you are filtering down to a single value-- the purpose of GROUP BY is to group rows of the same value together, so when you do WHERE userID = 999 GROUP BY userID it will only return one group (pointless).


$query = "SELECT PhotoID FROM Photos WHERE (Active = '0') AND (UserID = '$SessionUserID')";
$result = mysql_query($query) or die(mysql_error());

if (mysql_num_rows($result) > 10) {
print "You have reached your upload limit.";
print "When your submissions have been processsed,";
print "you will be able to resume uploading";
require ("Footer.php");
exit;
}

while($row = mysql_fetch_array($result))
{
.
.
.
}

StanLytle
08-08-2007, 10:13 PM
Thanks whizard and Fumigator. I went with Fumigator's code. I'm still trying to get a grip on this stuff, and if I get something to work, it's usually by guessing as in this case. The redundant code was leftovers from previous attempts.

I moved things around a bit, I put the "while" under the "$result", and the "if" under the "while". Don't know if that was a good thing or not, but it still works.

Thanks again,
Stan
:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum