PHP - SQL查询仅显示最后的结果

时间:2016-10-12 16:54:50

标签: php

这只返回我的最后一行:

<?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() ?>

如果我像这样回应它可以工作(返回所有行):

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

我还需要在函数infos_content()中更改以获取所有行? 非常感谢!

1 个答案:

答案 0 :(得分:2)

这是因为您为每个结果重新定义$ ouput

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

相反,在循环外创建$ ouput并附加到它...

{{1}}