tidus97 Posted January 21, 2009 Share Posted January 21, 2009 $query = mysql_query("SELECT id FROM comment WHERE pollId = '$inPollId' ORDER BY id DESC ") or die("Could not get comments"); $inPollId is 1. $row = mysql_fetch_assoc($query); echo print_r($row); this echo's Array ( [id] => 3 ) the comment table has the following: id text pollId userId createDate 1 test 1 1 1232507071 2 test 2 1 1 1232507071 3 test 3 1 1 1232507071 why doesnt it echo the 3 id's? Link to comment https://forums.phpfreaks.com/topic/141724-solved-query-returning-only-one-array/ Share on other sites More sharing options...
Zane Posted January 21, 2009 Share Posted January 21, 2009 id text pollId userId createDate 1 test 1 1 1232507071 2 test 2 1 1 1232507071 3 test 3 1 1 1232507071 why doesnt it echo the 3 id's? Because in your query, you have told it to order by id descending....meaning...it will put the third one at the top of that list there. also..... $row = mysql_fetch_assoc($query); echo print_r($row); This is not a loop, so it will only grab the first row and stop......That is why How do you fix it...like this while($row = mysql_fetch_assoc($query)) { echo print_r($row); } Link to comment https://forums.phpfreaks.com/topic/141724-solved-query-returning-only-one-array/#findComment-741923 Share on other sites More sharing options...
Philip Posted January 21, 2009 Share Posted January 21, 2009 Add a while statement: <?php $query = mysql_query("SELECT id FROM comment WHERE pollId = '$inPollId' ORDER BY id DESC ") or die("Could not get comments"); while($row = mysql_fetch_assoc($query)) echo $row['id']; ?> BTW, you don't need to do "echo print_r" - select one or the other. Echo will print "Array", and print_r will show the structure of the array. Link to comment https://forums.phpfreaks.com/topic/141724-solved-query-returning-only-one-array/#findComment-741924 Share on other sites More sharing options...
tidus97 Posted January 21, 2009 Author Share Posted January 21, 2009 wierd. ok i see. the problem seems to happen later down the track. the query does return 3 rows like i expected... so thats not the problem. anyway, here we go: //comment object//////////////////////////////// class CommentOb { public $id; public $creator; public $text; public $createDate; function __construct($inId=null) { if(!empty($inId)) { $this->id = $inId; $query = mysql_query("SELECT * FROM comment WHERE id = '$inId'"); $row = mysql_fetch_assoc($query); $text = $row["text"]; $this->text = CleanupOutput($text); $this->createDate = date("r",$inCreateDate); $query = mysql_query("SELECT username FROM user WHERE id = '".$row[userId]."'"); $row = mysql_fetch_assoc($query); $this->creator = $row["username"]; } } } //get comments//////////////////////////////// function GetComment ($inPollId=null,$inUserId=null) { if (!empty($inPollId)) { $query = mysql_query("SELECT id FROM comment WHERE pollId = '$inPollId' ORDER BY id DESC ") or die("Could not get comments"); } elseif (!empty($inUserId)) { $query = mysql_query("SELECT id FROM comment WHERE userId = '$inUserId' ORDER BY id DESC") or die("Could not get comments"); } else { echo "<div class='notice'>Vital information is missing!</div>"; return 0; } $commentArray = array(); $row = mysql_fetch_assoc($query); echo mysql_num_rows($query); // echos 3. good... if (mysql_num_rows($query) == 0) { echo "<div class='notice'>There are no comments!</div>"; return 0; } foreach ($row as $inRow) { $myComment = new CommentOb($inRow['id']); array_push($commentArray, $myComment); } return $commentArray; } $activePollComments = GetComment(1,0); //ive set the id as 1 to test. this should mean all the comments assigned to the poll with id 1. if ($activePollComments != 0) { foreach ($activePollComments as $currentComment) { echo "<div class='comment'><div class='commenthead'> ".$currentComment->creator." on ".$currentComment->createDate." </div> ".$currentComment->text."</div>"; } } } im expecting it to return all the comments with the pollId 1, (all 3 i posted in the above table), order by the highest id one first. however, i get only one comment displaying: the one with the highest id. hope this makes sense. i have snipped out the parts that control the poll itself, because it isnt a part of the problem. Link to comment https://forums.phpfreaks.com/topic/141724-solved-query-returning-only-one-array/#findComment-741934 Share on other sites More sharing options...
Zane Posted January 21, 2009 Share Posted January 21, 2009 Add a while statement: Link to comment https://forums.phpfreaks.com/topic/141724-solved-query-returning-only-one-array/#findComment-741935 Share on other sites More sharing options...
Philip Posted January 21, 2009 Share Posted January 21, 2009 This needs to be in a loop for it to pull more than the first row: $row = mysql_fetch_assoc($query); Link to comment https://forums.phpfreaks.com/topic/141724-solved-query-returning-only-one-array/#findComment-741938 Share on other sites More sharing options...
tidus97 Posted January 21, 2009 Author Share Posted January 21, 2009 err, it is. foreach ($row as $inRow) { $myComment = new CommentOb($inRow['id']); array_push($commentArray, $myComment); } sigh, im so confused right now Link to comment https://forums.phpfreaks.com/topic/141724-solved-query-returning-only-one-array/#findComment-741939 Share on other sites More sharing options...
Philip Posted January 21, 2009 Share Posted January 21, 2009 No, it's not. The mysql_fetch_assoc($query); needs to be in a loop - every time you call that, it pulls the current row from the database as an array. Right now, it's pulling the first row, and you're trying to do a loop on an array with one item in it. Try on: <?php while($row = mysql_fetch_assoc($query)) { $myComment = new CommentOb($row['id']); array_push($commentArray, $myComment); } ?> Link to comment https://forums.phpfreaks.com/topic/141724-solved-query-returning-only-one-array/#findComment-741944 Share on other sites More sharing options...
tidus97 Posted January 21, 2009 Author Share Posted January 21, 2009 dont you just love php Link to comment https://forums.phpfreaks.com/topic/141724-solved-query-returning-only-one-array/#findComment-741949 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.