voogah Posted March 21, 2010 Share Posted March 21, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/196013-using-the-avg-fuction/ Share on other sites More sharing options...
harristweed Posted March 21, 2010 Share Posted March 21, 2010 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/196013-using-the-avg-fuction/#findComment-1029592 Share on other sites More sharing options...
voogah Posted March 21, 2010 Author Share Posted March 21, 2010 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!! Quote Link to comment https://forums.phpfreaks.com/topic/196013-using-the-avg-fuction/#findComment-1029626 Share on other sites More sharing options...
harristweed Posted March 21, 2010 Share Posted March 21, 2010 what is line 27? Quote Link to comment https://forums.phpfreaks.com/topic/196013-using-the-avg-fuction/#findComment-1029700 Share on other sites More sharing options...
greatstar00 Posted March 21, 2010 Share Posted March 21, 2010 27 is mysql resource, not line number line #= 1 which means somewhere in the mysql query try to print the query out the part select avg(customer_id) ............... Quote Link to comment https://forums.phpfreaks.com/topic/196013-using-the-avg-fuction/#findComment-1029706 Share on other sites More sharing options...
harristweed Posted March 21, 2010 Share Posted March 21, 2010 Oh yes, doh! try changing $query = mysql_query("SELECT AVG(customer_service) FROM rating WHERE cust_id LIKE '$search'"); to $query = mysql_query("SELECT AVG(customer_service) FROM rating WHERE cust_id = '$search' "); Quote Link to comment https://forums.phpfreaks.com/topic/196013-using-the-avg-fuction/#findComment-1029707 Share on other sites More sharing options...
greatstar00 Posted March 21, 2010 Share Posted March 21, 2010 that is the same thing theoretically Quote Link to comment https://forums.phpfreaks.com/topic/196013-using-the-avg-fuction/#findComment-1029709 Share on other sites More sharing options...
Mchl Posted March 21, 2010 Share Posted March 21, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/196013-using-the-avg-fuction/#findComment-1029712 Share on other sites More sharing options...
voogah Posted March 21, 2010 Author Share Posted March 21, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/196013-using-the-avg-fuction/#findComment-1029754 Share on other sites More sharing options...
Andy-H Posted March 21, 2010 Share Posted March 21, 2010 $int = round($int, 0); round You can also use, $result = mysql_query($query)or trigger_error(mysql_error() . ' On line: ' . __LINE__); trigger_error Quote Link to comment https://forums.phpfreaks.com/topic/196013-using-the-avg-fuction/#findComment-1029766 Share on other sites More sharing options...
Mchl Posted March 22, 2010 Share Posted March 22, 2010 SELECT ROUND(AVG(customer_service)) FROM rating WHERE cust_id = '$search' Quote Link to comment https://forums.phpfreaks.com/topic/196013-using-the-avg-fuction/#findComment-1029876 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.