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
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";
?>

Link to comment
Share on other sites

 

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!!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Mchl was correct!!

 

Once I took mysql_query out it worked!

 

Now, how do I get it to round to the nearest whole number?  Right now I it comes out as 8.333 but I would like it to be 8.

 

Thank everyone for the quick help!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.