Reply
MySQL PHP get average value for column
Old 05-30-2008, 11:50 AM MySQL PHP get average value for column
drew22299's Avatar
Skilled Talker

Posts: 88
Location: Wiltshire, UK
Hi,

I have tried different ways of doing this but still can't get the average value for the column VehCondition, can anyone see what I'm doing wrong? It doesn't return a value for $ConditionRating;

PHP Code:

$Vehicles2
=mysql_fetch_array("SELECT avg( VehCondition ) as ConditionRating, Type FROM vehicles where Userid='$_SESSION[userid]' GROUP BY Type"); 

$ConditionRating=$Vehicles2['ConditionRating'];
echo 
$ConditionRating
__________________
www.hotlista.co.uk
drew22299 is offline
Reply With Quote
View Public Profile Visit drew22299's homepage!
 
When You Register, These Ads Go Away!
     
Old 05-30-2008, 02:13 PM Re: MySQL PHP get average value for column
Super Talker

Posts: 119
try doing it with....

PHP Code:
$Vehicles2=mysql_fetch_array(mysql_query("SELECT avg( VehCondition ) as ConditionRating, Type FROM vehicles where Userid='$_SESSION[userid]' GROUP BY Type")); 
forgot the mysql_query
</span></span>
kbfirebreather is offline
Reply With Quote
View Public Profile
 
Old 05-30-2008, 03:47 PM Re: MySQL PHP get average value for column
drew22299's Avatar
Skilled Talker

Posts: 88
Location: Wiltshire, UK
There were no results returned from the query, the vehicle type can be either trains or road vehicles so if the query is grouping the results by type, would it need to be output differently?

Basically, I want to get the condition rating for all vehicles of type train. Is this query similar to what it should be?

PHP Code:
$Vehicles2=mysql_fetch_array(mysql_query("SELECT avg( VehCondition ) as ConditionRating FROM vehicles where Userid='$_SESSION[userid]' and Type='Train'"));
 
$VehCondition=$Vehicles2['ConditionRating'];
 
echo 
$VehCondition
__________________
www.hotlista.co.uk
drew22299 is offline
Reply With Quote
View Public Profile Visit drew22299's homepage!
 
Old 05-30-2008, 05:40 PM Re: MySQL PHP get average value for column
tristanperry's Avatar
Average Talker

Posts: 15
Name: Tristan Perry
Location: United Kingdom
What do you get when you do:

PHP Code:
$query mysql_query("SELECT avg( VehCondition ) as ConditionRating FROM vehicles where Userid='$_SESSION[userid]' and Type='Train'") or die(mysql_error())

echo 
mysql_num_rows($query); 
Either there's an error with your query (in which case the "mysql_error()" bit will tell you what's wrong), or it'll (probably) output that 0 records were returned - meaning your MySQL is syntactically correct, however there's a flaw in the query's logic.
__________________
|| The Legion Host
|| Shared and Reseller hosting
|| cPanel | Daily off-site backups | Quality Support
tristanperry is offline
Reply With Quote
View Public Profile Visit tristanperry's homepage!
 
Old 05-30-2008, 06:06 PM Re: MySQL PHP get average value for column
drew22299's Avatar
Skilled Talker

Posts: 88
Location: Wiltshire, UK
Thanks for your suggestions, the first error it returned was that there wasn't a vehicles table and the error was that Vehicles should have a capital letter.

I tried it again and it returned 1 but not sure why because there are two vehicles where type=train in the database.

I tried outputing the result but it was still empty.

The vehicles in the Vehicle table have VehCondition ratings between 1 - 100, how can I get the vehicle ratings and get the average of them?
__________________
www.hotlista.co.uk
drew22299 is offline
Reply With Quote
View Public Profile Visit drew22299's homepage!
 
Old 05-30-2008, 07:26 PM Re: MySQL PHP get average value for column
tristanperry's Avatar
Average Talker

Posts: 15
Name: Tristan Perry
Location: United Kingdom
Hmm, I'm not 100% sure. Without knowing the data and table structures, I can't say I'm all that sure.

It might not, although does:

http://www.tizag.com/mysqlTutorial/mysqlavg.php

Shed any new light on things (it's a user-friendly approach to the avg() function)?
__________________
|| The Legion Host
|| Shared and Reseller hosting
|| cPanel | Daily off-site backups | Quality Support
tristanperry is offline
Reply With Quote
View Public Profile Visit tristanperry's homepage!
 
Old 05-31-2008, 01:48 AM Re: MySQL PHP get average value for column
Arenlor's Avatar
Ultra Talker

Posts: 463
Name: Jerod Lycett
Location: /home/arenlor
Is there any way you can export the mysql table for us and post it here?
__________________
PHP Code:
<?php echo "Hello World"?>
HTML Code:
<html><head><title>Hello World</title></head><body><p>Hello World</p></body></html>
Arenlor is offline
Reply With Quote
View Public Profile Visit Arenlor's homepage!
 
Old 05-31-2008, 05:42 AM Re: MySQL PHP get average value for column
drew22299's Avatar
Skilled Talker

Posts: 88
Location: Wiltshire, UK
tristanperry, thanks for the link but that was the one I used originally

Arenlor, the table consists of the following:

PHP Code:
Vehicleid int (autonumber), PK
Userid varchar
VehicleName varchar
Type varchar
VehCondition int 
Thanks,
__________________
www.hotlista.co.uk
drew22299 is offline
Reply With Quote
View Public Profile Visit drew22299's homepage!
 
Old 05-31-2008, 11:32 AM Re: MySQL PHP get average value for column
Super Talker

Posts: 119
Is UserID unique?

Cause....is the person doing this, have a Type 'Train'?

IMO, I feel you're getting the average of one row of data with that query.

Try it with this...

PHP Code:
$Vehicles2=mysql_fetch_assoc(mysql_query("SELECT avg( VehCondition ) as ConditionRating FROM vehicles where Type='Train'")); 
I took out the userID requirement of the query, and made it a fetch_assoc.

Try this with fetch_array and fetch_assoc, see if those two seem to work.
</span></span>
kbfirebreather is offline
Reply With Quote
View Public Profile
 
Old 05-31-2008, 02:21 PM Re: MySQL PHP get average value for column
Arenlor's Avatar
Ultra Talker

Posts: 463
Name: Jerod Lycett
Location: /home/arenlor
Type is case sensitive, make sure you've entered it correctly.
__________________
PHP Code:
<?php echo "Hello World"?>
HTML Code:
<html><head><title>Hello World</title></head><body><p>Hello World</p></body></html>
Arenlor is offline
Reply With Quote
View Public Profile Visit Arenlor's homepage!
 
Old 06-01-2008, 10:32 AM Re: MySQL PHP get average value for column
drew22299's Avatar
Skilled Talker

Posts: 88
Location: Wiltshire, UK
Thanks kbfirebreather and Arenlor, the query worked
__________________
www.hotlista.co.uk

Last edited by drew22299 : 06-01-2008 at 10:36 AM.
drew22299 is offline
Reply With Quote
View Public Profile Visit drew22299's homepage!
 
Old 06-01-2008, 11:47 AM Re: MySQL PHP get average value for column
Novice Talker

Posts: 7
Name: Nitin
Location: INDIA
$query = mysql_query("SELECT avg( VehCondition ) as ConditionRating FROM vehicles where Type='Train'") or die(mysql_error())

echo $ConditionRating
;
nitin44u is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to MySQL PHP get average value for column
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.16433 seconds with 13 queries