Jump to content

Using the AVG fuction


voogah

Recommended Posts

Hello All, New to PHP FREAKS! 

 

I am trying to get an average of a column in a database.  The column is called "product_quality".  I also have a column called "cust_id" that is the customer id's. 

 

When I get the average I want it to be for just a certain customer id.

 

I thought I had the code correct but it isn't printing it out right.  Here is what I have!

 

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","user","password"); 

//select which database you want to edit
mysql_select_db("voogahco_microblog"); 

$search="$id";

//get the mysql and store them in $result
//change whatevertable to the mysql table you're using
//change whatevercolumn to the column in the table you want to search
// Make a MySQL Connection

$query = mysql_query("SELECT  AVG(customer_service) FROM rating WHERE cust_id LIKE '$search'"); 

$result = mysql_query($query) or die(mysql_error());

echo $result
?>

 

Any help would be appreciated!

Link to comment
https://forums.phpfreaks.com/topic/196013-using-the-avg-fuction/
Share on other sites

try

<?php// dont use short tags!!!

//connect to mysql
//change user and password to your mySQL name and password
$link_id=mysql_connect("localhost","user","password"); 
    
//select which database you want to edit
if (mysql_select_db("voogahco_microblog", $link_id));


$search="$id";

//get the mysql and store them in $result
//change whatevertable to the mysql table you're using
//change whatevercolumn to the column in the table you want to search
// Make a MySQL Connection

$query = mysql_query("SELECT  AVG(customer_service) FROM rating WHERE cust_id LIKE '$search'"); 
     
$result = mysql_query($query) or die(mysql_error());

$row=mysql_fetch_row($result);

$average=$row[0];

echo"average=$average";
?>

 

Thank you, I made changes according to your post and I get this message?

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #27' at line 1

 

Any suggestions!

 

Thank you!!

You're using mysql_query twice:

 

Try like this

$query = "SELECT  AVG(customer_service) FROM rating WHERE cust_id = '$search'"; 
$result = mysql_query($query) or die(mysql_error());

 

Where do $id and $search variables come from? Are you using register_globals?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.