Jump to content

Recommended Posts

$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

  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);
   }

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.

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.

 

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);
}
?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.