Solved

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

Posted on 2009-07-01
1
526 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
ID: 24755805
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL recovery 7 27
Formating field in mysql Advance formatting 1 38
Increase counter and attr inside a while loop 15 34
PHP SMTP authentication 6 23
Creating and Managing Databases with phpMyAdmin in cPanel.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

679 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