homepage Welcome to WebmasterWorld Guest from 54.167.238.60
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
mysql - select average col value then update table
Wheres the flaw, simple solution.
dkin




msg:3357129
 7:14 am on Jun 3, 2007 (gmt 0)

Hey everyone, Im trying to use 2 tables at the same time, one houses comments, the other the main info for my product, now, on the comments table it has ratings, so what Im trying to do is when someone submits a rating I want to get the average for all ratings, then update my other table with the info, this is what I've written, but it doesn't seem to be working.

mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
$presult = mysql_query("SELECT AVG(rating) FROM comments where gid = '$gid'") or die ("query 1: " . mysql_error());
$prow = mysql_fetch_array($presult) or die ("query 1: " . mysql_error());
$result = mysql_query("UPDATE games SET rating = '$prow[rating]' WHERE id = $gid") or die(mysql_error());

Like I said its prolly a simple solution, but its 3 am here lol.

Thanks all

Dylan

 

Habtom




msg:3357133
 7:18 am on Jun 3, 2007 (gmt 0)

Try the following:

$presult = mysql_query("SELECT AVG(rating) as avg_rating FROM comments where gid = '$gid'") or die ("query 1: " . mysql_error());
$prow = mysql_fetch_array($presult) or die ("query 1: " . mysql_error());
$result = mysql_query("UPDATE games SET rating = '". $prow[avg_rating] ."' WHERE id = '". $gid ."'") or die(mysql_error());

I assume $gid is passed by well.

Habtom

[edited by: Habtom at 7:19 am (utc) on June 3, 2007]

dkin




msg:3357135
 7:23 am on Jun 3, 2007 (gmt 0)

excellent, this makes everything so much easier, thank you.

Dylan

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved