GreenUser Posted June 24, 2008 Share Posted June 24, 2008 Hi all, currently line 30 is not producing anything. The results are the same if I use 'echo $avgrow->id;' PHP Notice: Undefined index: id in C:\\Program Files\\Apache\\htdocs\\ratemydate\\percent.php on line 6 PHP Notice: Trying to get property of non-object in C:\\ProgramFiles\\Apache\\htdocs\\ratemydate\\percent.php on line 30 I played around for a while with the sql and in phpmyadmin I do get results I want, I just need to get them to display, not sure what I am doing wrong. <?php require("config.php"); require("functions.php"); if(pf_check_number($_GET['id']) == TRUE) { $validid = $_GET['id']; } else { $validid = 21; } require("header.php"); $sql = "SELECT * FROM stories WHERE id = " . $validid . ";"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); echo "<h1>" . $row['location'] . "</h1>"; echo nl2br($row['body']); $avgsql = "SELECT SUM(fun + soh + con + sma + sex + hyg + mes) FROM stories WHERE id = " . $validid . ";"; $avgresult = mysql_query($avgsql); $avgrow = mysql_fetch_array($avgresult); echo "<p>"; echo "<strong>Rating</strong>: "; echo $avgrow['id']; echo "</p>"; require("footer.php"); ?> Link to comment https://forums.phpfreaks.com/topic/111733-solved-unpacking-an-array-results-negative/ Share on other sites More sharing options...
DarkWater Posted June 24, 2008 Share Posted June 24, 2008 You never actually pull "id" out of the database. And the second error only occurs when you use $avgrow->id because $avgrow isn't an object. Link to comment https://forums.phpfreaks.com/topic/111733-solved-unpacking-an-array-results-negative/#findComment-573558 Share on other sites More sharing options...
GreenUser Posted June 24, 2008 Author Share Posted June 24, 2008 Okay, with what $avgrow is set to it won't work then. Not sure that the 'id' is so important at this step, just trying to get the info out of the db and onto the page. Link to comment https://forums.phpfreaks.com/topic/111733-solved-unpacking-an-array-results-negative/#findComment-573570 Share on other sites More sharing options...
DarkWater Posted June 24, 2008 Share Posted June 24, 2008 I'd change the query to: $avgsql = "SELECT SUM(fun + soh + con + sma + sex + hyg + mes) AS total FROM stories WHERE id = " . $validid . ";"; Then $avgrow['total'] will work as the sum. Link to comment https://forums.phpfreaks.com/topic/111733-solved-unpacking-an-array-results-negative/#findComment-573572 Share on other sites More sharing options...
GreenUser Posted June 24, 2008 Author Share Posted June 24, 2008 Thank you Darkwater. AS more or less sets 'total' as a variable? It was beautiful to see the number 35 finally display! Very good, thank you. Link to comment https://forums.phpfreaks.com/topic/111733-solved-unpacking-an-array-results-negative/#findComment-573577 Share on other sites More sharing options...
DarkWater Posted June 24, 2008 Share Posted June 24, 2008 AS is used to create aliases in MySQL. You basically need to create an alias for any function returns unless you want to use the numerically indexed array. Also, aliases are good for something like: SELECT u.userid AS uid FROM users as u, friends as f WHERE f.userid = $id AND f.friendid = u.userid; Link to comment https://forums.phpfreaks.com/topic/111733-solved-unpacking-an-array-results-negative/#findComment-573593 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.