Jump to content


Photo

avg() function from php


  • Please log in to reply
3 replies to this topic

#1 infophiliac

infophiliac
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 14 July 2006 - 03:25 PM

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.

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 15 July 2006 - 12:14 AM

Very strange... it can't return nothing!
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 infophiliac

infophiliac
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 16 July 2006 - 09:05 PM

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.

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 16 July 2006 - 09:07 PM

Sounds like you were getting back a hash in PHP, and had no way to properly reference the expression column.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users