Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

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: 93
Trades: 0
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
drew22299 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-30-2008, 02:13 PM Re: MySQL PHP get average value for column
Extreme Talker

Posts: 177
Trades: 0
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: 93
Trades: 0
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
drew22299 is offline
Reply With Quote
View Public Profile
 
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
Trades: 0
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.
__________________
||
Please login or register to view this content. Registration is FREE

|| 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: 93
Trades: 0
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?
drew22299 is offline
Reply With Quote
View Public Profile
 
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
Trades: 0
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)?
__________________
||
Please login or register to view this content. Registration is FREE

|| 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: 462
Name: Jerod Lycett
Location: /home/arenlor
Trades: 0
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: 93
Trades: 0
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,
drew22299 is offline
Reply With Quote
View Public Profile
 
Old 05-31-2008, 11:32 AM Re: MySQL PHP get average value for column
Extreme Talker

Posts: 177
Trades: 0
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: 462
Name: Jerod Lycett
Location: /home/arenlor
Trades: 0
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: 93
Trades: 0
Thanks kbfirebreather and Arenlor, the query worked

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

Posts: 7
Name: Nitin
Location: INDIA
Trades: 0
$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 Search this Thread
Search this Thread:

Advanced Search

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

BB 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.33406 seconds with 12 queries