Jump to content

avg() function from php


infophiliac

Recommended Posts

I'm trying to send the following query from a page written with PHP:

$query = "SELECT AVG(rating) FROM ratings WHERE rtime >= SUBDATE(NOW(), INTERVAL 2 HOUR) AND id=1";

When I run the query from the MySQL command line, it comes back with exactly what it should.  When I run the page with that query, it returns nothing (including no error message).  I've debugged to the point where I know that the query works if I take out AVG(), but can't for the life of me see why.

I'm running MySQL 4.0 and PHP 4.4.1 on FreeBSD.

Any insights would be appreciated.
Link to comment
Share on other sites

I haven't actually managed to wrap my head around all the why and wherefore, but I did solve the problem.  The average needed to be assigned an alias, so "SELECT AVG(rating)..." became "SELECT AVG(rating) as r."  Like I said, I'm still figuring out why that is so, but the immediate issue is resolved.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.