TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   a question , alittle of math here (http://www.talkphp.com/absolute-beginners/1928-question-alittle-math-here.html)

webtuto 01-10-2008 05:21 PM

a question , alittle of math here
 
hi
whats the error here =>

PHP Code:

$web="SELECT SUM(`rate`) / COUNT(`rate`) from `rate`";
$tuto=mysql_query($web) or die('Database error: ' mysql_error());
$mas=mysql_fetch_array($tuto);
echo
"The Rating of this tutorial is : ".$mas['rate']."<br><br>"

it douesnt show the result :s

xenon 01-10-2008 05:28 PM

You mean in SQL or what? If SQL is your case...(MySQL for this matter):

Code:

SELECT SUM(column_name)/COUNT(column_name)

webtuto 01-10-2008 05:32 PM

thanks i did it but how to show to result ?

SOCK 01-10-2008 05:44 PM

Please, you're going to have to provide more details. Your messages are vague at best. Also, please read some of the links I've provided in replies to your other threads.

webtuto 01-10-2008 06:23 PM

it gives this error
Database error: Unknown column 'rate' in 'field list'

even if in the table "rate" there is a column named "rate"

SOCK 01-10-2008 06:49 PM

You need to use an ALIAS on that result, e.g.
Code:

SELECT SUM(`rate`) / COUNT(`rate`) AS average_rate from `rate`
Refer to the alias average_rate in your PHP code, i.e. $mas['average_rate'].

This is why we ask you to be more specific; the query xenon provided by example works fine as-is in MySQL, but not if you want to return the value in PHP.

I would also suggest, if you're retrieving only one column from a resultset, use mysql_result() instead of one of the mysql_fetch_* functions. You don't need an array, there is only a single row and single column result output.

webtuto 01-10-2008 06:58 PM

yeah i solve it thanks


All times are GMT. The time now is 05:55 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0