1

This only returns the last one of my rows:

<?php
				function all_infos_query() {
					global $connection;
					
					$query  = "SELECT * ";
					$query .= "FROM pages ";
					$query .= "JOIN subjects ";
					$query .= "ON pages.subject_id=subjects.id";
					$infos_set = mysqli_query($connection, $query);
					confirm_query($infos_set);
					return $infos_set;
				}
			
				
				function infos_content() {
					$infos_set = all_infos_query();		
					
					while($info = mysqli_fetch_assoc($infos_set)) {
						$output = htmlentities($info["content"]);
						$output .= " <br><br>";
					}
					mysqli_free_result($infos_set);
					return $output;
					
				}
			?>
            
            <?php echo infos_content() ?>

If I echo it like this it works (returns all rows):

<?php 
$result = all_infos_query();

if($result === FALSE) { 
	echo "query failed: " . mysqli_error($connection);
}
else {
	while($row = mysqli_fetch_array($result)) {
		echo htmlentities($row['content']);
		echo "<br><br>";
	}
}
?>

What do I have to change in the function infos_content() to also get all the rows? Thanks a lot!

1 Answer 1

2

This is because you redefine $ouput for every result

while($info = mysqli_fetch_assoc($infos_set)) {
                    $output = htmlentities($info["content"]); 
                    $output .= " <br><br>";

Instead, create $ouput outside the loop and append to it..

 $output ='';
while($info = mysqli_fetch_assoc($infos_set)) {
                    $output .= htmlentities($info["content"]); 
                    $output .= " <br><br>";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.