/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] SUM query trouble

Hello,
I’m trying to run a query that will check the total amount of space a user has taken up with their file uploads and enforce a limit to that amount of space. I’ve already setup the upload handler to insert file name and location, as well as username and file size into a mysql table. All of that works properly and the data gets correctly stored. So, my query runs in the upload handler before anything other than the session/page protection. This is what I’ve come up with:

[code=php]$mine = ($_SESSION[‘user_name’]);
$limit = mysql_query(“SELECT SUM(‘fsize’) FROM UploadedFiles WHERE username = ‘$mine'”);
if ($limit => “10485760”) die(“You have exceeded your storage limit.”);[/code]

When I add this to the upload handler, the upload form seems to work, but hitting “submit” loads a blank page with the URL pointed at “upload.processor.php”. Error handling is set to ‘all’, but no errors are showing up, and if from that blank page, you try to view source, that’s blank too. I’ve been searching this all day and can’t figure out why it won’t work, although, this is the first time I’ve tried to use the SUM function. The file size shouldn’t be an issue as I’ve set it in php.ini and on the html form, to 10mb. I’ve tried about 10 different variations of this and none of them work. Any suggestions?
Thanks

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@wsparrowauthorJan 19.2011 — Anybody?
Copy linkTweet thisAlerts:
@thraddashJan 19.2011 — Don't place column names within quotes...

Also mysql_query only returns a link identifier to your recordset. You still need to extract the column from the record...

[code=php]$mine = ($_SESSION['user_name']);
$query_id = mysql_query("SELECT SUM(fsize) AS total FROM UploadedFiles WHERE username = '$mine'");
if ($item = mysql_fetch_assoc($query_id)) {
if ($item['total'] >= 10485760) die ("You have exceeded your storage limit.");
}
mysql_free_result($query_id);[/code]


Untested, but thats the idea.
Copy linkTweet thisAlerts:
@tirnaJan 19.2011 — I've been searching this all day and can't figure out why it won't work, [/quote]

1) You can't use single or double quotes around the table column name. You can however use the back-tick ` (on the same key as ~ on my keyboard) but it's not necessary.

2) had you searched mysql_query in the online php manual you would have found the answer to the incorrect way you are using $limit. Bookmarking the php manual in your browser will result in a handy quick to reach reference.

3) when you have problems running a query, a good idea is display the evaluated query in the browser

[code=php]
$query = 'select * from users where userId = "'.$username.'"';

echo $query; die();
[/code]


Normally you should be able to pick up the error(s) in it pretty quickly, but if they are not obvious you can copy and paste the evaluated query into an sql gui interface like the free and very good SQLyog where it should be much easier to debug.
Copy linkTweet thisAlerts:
@wsparrowauthorJan 19.2011 — Thanks guys, I did try it without the quotes and had the same results, but I'll try those other suggestions when I get home.
Copy linkTweet thisAlerts:
@wsparrowauthorJan 20.2011 — Okay, tested.

@thraddash: Thank you, that worked like a charm.

@tirna: I get the point you're trying to make but as I stated originally I had tried this many different ways before I posted it, and I only posted my latest attempt. I had also spent most of a day on php.net, this forum, and google. Nothing I found was working. But thanks all the same.
Copy linkTweet thisAlerts:
@tirnaJan 20.2011 — no problem ?

I'm not sure which pages on php.net you looked at but the link I posted takes you the page on php.net that shows you how to extract data from a database using mysql_query and mysql_fetch_assoc.
×

Success!

Help @wsparrow spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...