Solved

MySQL MAX(), AVG(), and MIN() not working in PHP

Posted on 2009-07-01
1
509 Views
Last Modified: 2013-12-13
Hey,

Whenever I run this script something is going wrong because the max(), avg(), and min() values are coming up as 0. I have run is_null() on all of them and they are not NULL. They are just coming up as 0, but they shouldn't be. 1 vote has been "4" and 1 vote has been "2" so I should be coming up with an avg of 3, max of 4 and min of 2. I can only get COUNT() to work in the statement below, which is giving me the correct answer of 2.

Thanks. Any help is appreciated.
$get_advice_sql = "SELECT a.ID, a.person_ID, a.type, a.keywords, a.title, a.information, a.effect_start, Count(v.vote), Avg(v.vote), Max(v.vote), Min(v.vote), u.clsUserName

				FROM articles as a 

				LEFT JOIN USERTABLE as u ON u.clsUserID = a.person_id

				LEFT JOIN votes as v ON a.ID = v.article_ID

				WHERE a.type = 'advice'

				AND a.ID = (SELECT max(ID) 

							FROM articles

							WHERE type = 'advice')

				GROUP BY a.ID";

	if ($result = mysqli_query($mysqli, $get_advice_sql)) {

		while ($row = mysqli_fetch_row($result)) {

				$advice_id = $row[0];

				$title = $row[4];

				$information = $row[5];

				$effect_start = $row[6];

				$votes_count = $row[7];

				$votes_average = $row[8];

				$votes_max = $row[9];

				$votes_min = $row[10];

				$author = $row[11];		

		}

	}

Open in new window

0
Comment
Question by:Sucao
1 Comment
 

Accepted Solution

by:
Sucao earned 0 total points
Comment Utility
Sorry, I posted this question and realized that it was actually a problem while I was putting the initial data into MySQL. I was putting in the votes as 0's so of course it was coming up with 0 for the max, min, and average!

Feeling like an idiot :/

Sorry guys
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now