douruyun8153 2014-03-23 16:53
浏览 32

基本PHP While循环显示新行上的每个记录

I am trying to return a basic list from a table in PHP but for some reason it returns the results in one long string instead of displaying the next record on a new line. I have done something similar a while ago but I don't use PHP that often so I'm guessing I am missing a fairly obvious mistake. Any help is appreciated.

My current code looks like this.

<?php

require_once("dbconnect.php"); 

$query = "SELECT * FROM clients";

$dbRecords = mysql_query($query, $dbConnect) 
or die("Problem reading table: " + mysql_error());

$clientName = $dbRecords["clientName"];

while ($arrRecords = mysql_fetch_array($dbRecords)) 
{
     $clientName .= $arrRecords["clientName"];
}

?>

<div>
<?php
     echo '<h2>Client List</h2>';
     echo $clientName; echo'<br />'; echo'<br />';
?>
</div>
  • 写回答

4条回答 默认 最新

  • dplbf4340 2014-03-23 16:55
    关注

    Don't do

    $clientName = $dbRecords["clientName"];
    

    because $dbRecords is the resultset resource; just initialise $clientName to an empty string, then loop

    $clientName = '';
    
    while ($arrRecords = mysql_fetch_array($dbRecords)) {
         $clientName .= $arrRecords["clientName"] . '<br />';
    }
    

    adding the newline (<br />) in your loop

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效