barocky82 Posted March 18, 2011 Share Posted March 18, 2011 Hi Guys, I'm a PHP newbie and I'm having some trouble creating a function that I can call where I can perform a MYSQL SELECT querey which returns an associative array, and be able to pull a value from that array and return it to the call. My code is below. Everything seems to work up until where I try to set the $average variable. If I return $row instead of $round_val, I see me array as "Array ( [user_id] => 64 [AVG(twos_made)] => 5.0000 )" It seems as though my my functions arugment (twos_made - which is my $column_name var) is not getting passed through or something. Any help would be greatly appreciated! Thanks! function avg_of($column_name) { global $connect_db; $query = "SELECT user_id, AVG($column_name) FROM table GROUP BY user_id"; $result = mysql_query($query, $connect_db); $row = mysql_fetch_assoc($result); $average = $row['AVG($column_name)']; $round_val = round($average, 1); return $round_val; } Link to comment https://forums.phpfreaks.com/topic/231025-pull-value-from-array-w-key-variable-function-not-working/ Share on other sites More sharing options...
kenrbnsn Posted March 18, 2011 Share Posted March 18, 2011 Change your query to <?php $query = "SELECT user_id, AVG($column_name) as my_avg FROM table GROUP BY user_id"; ?> Then in your code you can do <?php $average = $row[' my_avg']; ?> Ken Link to comment https://forums.phpfreaks.com/topic/231025-pull-value-from-array-w-key-variable-function-not-working/#findComment-1189255 Share on other sites More sharing options...
barocky82 Posted March 18, 2011 Author Share Posted March 18, 2011 Thanks for the the quick response, Ken! Unfortunately, that didn't do the trick either. Here is what my code looks like now per your request: function avg_of($column_name) { global $connect_db; $query = "SELECT user_id, AVG($column_name)as my_avg FROM table GROUP BY user_id"; $result = mysql_query($query, $connect_db); $row = mysql_fetch_assoc($result); $average = $row[' my_avg']; $round_val = round($average, 1); return $row; I'm not getting a zero as the average when it should be '5'. If I return $row instead of $round_val, I get "Array ( [user_id] => 64 [my_avg] => 5.0000 )". For some reason it doesn't seem like the extraction of the 'my_avg' key from the $row array is working. Any other thoughts? Thanks again for you help! Link to comment https://forums.phpfreaks.com/topic/231025-pull-value-from-array-w-key-variable-function-not-working/#findComment-1189263 Share on other sites More sharing options...
Pikachu2000 Posted March 18, 2011 Share Posted March 18, 2011 $average = $row[' my_avg']; An extra space is ^ there. [yoda]It you must remove![/yoda] Do you need both of the raw values from the query result returned, as well as the rounded average, or just the rounded average? Link to comment https://forums.phpfreaks.com/topic/231025-pull-value-from-array-w-key-variable-function-not-working/#findComment-1189266 Share on other sites More sharing options...
barocky82 Posted March 18, 2011 Author Share Posted March 18, 2011 I stand corrected. It worked perfectly....I found a space in $average = $row[' my_avg']; . I removed the space before the 'M' and I'm all set. Thanks a bunch!! Link to comment https://forums.phpfreaks.com/topic/231025-pull-value-from-array-w-key-variable-function-not-working/#findComment-1189267 Share on other sites More sharing options...
barocky82 Posted March 18, 2011 Author Share Posted March 18, 2011 Thanks for the help Pikachu2000! I just need the rounded result. Link to comment https://forums.phpfreaks.com/topic/231025-pull-value-from-array-w-key-variable-function-not-working/#findComment-1189270 Share on other sites More sharing options...
Pikachu2000 Posted March 18, 2011 Share Posted March 18, 2011 OK, that's what I thought. You're good to go. Link to comment https://forums.phpfreaks.com/topic/231025-pull-value-from-array-w-key-variable-function-not-working/#findComment-1189271 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.