Php 如何在mysql查询中获取多条记录以显示在网页中?

Php 如何在mysql查询中获取多条记录以显示在网页中?,php,mysql,Php,Mysql,我知道这是一个愚蠢的问题,我也试过看其他答案,但它们对我不起作用。我已经接管了维护一个包含大量预先存在的代码的站点的工作,需要添加一个新的查询 我已经设法让查询工作并显示在网页中,但只显示第一条记录。我知道不知何故我丢失了循环函数,但我不知道如何将它添加到函数中,以便显示所有记录 我已经为此奋斗了好几个小时,如果能给我一些建议,我将不胜感激 下面是主listing.php文件中的代码-第一个查询是预先存在的,新的查询是以“$sql2=”CALL sp_getFeedback(“.$listing

我知道这是一个愚蠢的问题,我也试过看其他答案,但它们对我不起作用。我已经接管了维护一个包含大量预先存在的代码的站点的工作,需要添加一个新的查询

我已经设法让查询工作并显示在网页中,但只显示第一条记录。我知道不知何故我丢失了循环函数,但我不知道如何将它添加到函数中,以便显示所有记录

我已经为此奋斗了好几个小时,如果能给我一些建议,我将不胜感激

下面是主listing.php文件中的代码-第一个查询是预先存在的,新的查询是以“$sql2=”CALL sp_getFeedback(“.$listing['unique_listing_id'])”开始的getFeedback过程“;”

if($PMDR->get('Authentication')->checkPermission($PMDR->get('Session')->get('user_id')){
$admin\u user\u can\u view\u prices=true;
$template\u content->set($admin\u user\u can\u view\u prices',$admin\u user\u can\u view\u prices);
$sql=“致电sp_艺人_联系人信息(“.$listing['unique_listing_id']”);
//所有变量都来自db.php
$mysqli=newmysqli($dbserver、$dbuser、$dbpass、$dbnameaf);
$rs=$mysqli->query($sql);
而($row=$rs->fetch_assoc())
{      
$template_content->set('primaryEmail',$row['primaryEmail']);
$template_content->set('secondary-email',$row['secondary-email']);
$template_content->set('mobile',$row['mobile']);
$template_content->set('homePhone',$row['homePhone']);
$template_content->set('alternatePhone',$row['alternatePhone(请指定)');
$template_content->set('streetAddress',$row['streetAddress']);
$template_content->set('entertercity',$row['City']);
$template_content->set('艺人状态',$row['State']);
$template_content->set('enterterzip',$row['Zip']);
$template_content->set('entertiernotes',$row['Note']);
}
$data=“”;
$griddata=“”;
$grid=“”;
$EntertierID=$\u POST['unique\u listing\u id'];
$mysqli=newmysqli($dbserver、$dbuser、$dbpass、$dbnameaf);
$sql2=“调用sp_GetFeedback(“.$listing['unique_listing_id']”);
$rs=$mysqli->query($sql2);
$numrows=mysqli_num_rows($rs)。“rows”;
而($row=$rs->fetch_assoc()){
$allRows[]=$row;
$style=($altRowCount%2==0?'class=“row”:'class=“row tdg”);
$template_content->set('eventID',$row['eventID']);
$template_content->set('dateOfEvent',$row['dateOfEvent']);
$template_content->set('clientFollow',$row['ClientFollowUp']);
$template_content->set('performerFollow',$row['PerformerFollowUp']);
$griddata.=”
“$eventID。”
“$dateOfEvent。”
“$clientFollow。”
“$performerFollow。”
";
$altRowCount=$altRowCount+1;
}
$mysqli->close();
$grid=”
“$griddata。”
";  
echo$gridHeader.$grid;
}//GetEnterterFeedback
}//是否有office用户登录?--结束
以下是包含网页HTML的included listing.tpl文件中的代码:

         <div class="row">
        <div class='col-xs-2 col-sm-2 col-md-2' onclick="popupEvent(".$row['eventID'].")"><?php echo $eventID ?></div>
        <div class='col-xs-2 col-sm-2 col-md-2'><?php echo $dateOfEvent ?></div>
        <div class='col-xs-4 col-sm-4 col-md-4'><?php echo $clientFollow ?></div>
        <div class='col-xs-4 col-sm-4 col-md-4'><?php echo $performerFollow ?></div>
      </div>

我试着加上

<?php
        foreach ($allRows as $row) {
    ?>

          <?php  }  ?>

但这只会导致根本不显示任何记录,甚至一条记录也不显示

由于您必须登录才能查看此数据,因此我无法共享此链接,但以下是蓝色登录区域的外观,蓝色框底部显示一条记录:

ETA:仅供参考-我也尝试在我的html代码中回显.grid-但这根本没有给我结果,不知道为什么

非常感谢任何能为我指明正确方向或告诉我我错过了什么的帮助


谢谢

首先,您已经打开了两次数据库连接

$data = "";
$griddata = "";
$grid = "";
$entertainerid = $_POST['unique_listing_id'];

# remove this line, just to be sure it doesn't cause any problems
$mysqli = new mysqli($dbserver, $dbuser, $dbpass, $dbnameaf);

$sql2 = "CALL sp_GetFeedback(".$listing['unique_listing_id'].")";
其次,通过在while循环中打印一些内容来调试查询结果,以确保查询返回多行作为结果

为了安全起见,我应该尝试的最后一件事是在while循环之前将$allRows声明为数组

 $allRows = array();
  while($row = $rs->fetch_assoc()) {
      $allRows[] = $row;
然后试试这个方法

  <?php
      foreach ($allRows as $row) {
      ?>
     <!-- HTML -->
      <?php  }  ?>

有一件事我不明白,那就是你不需要新的mysqli两次你是说打两次电话给DB吗?是 啊但我担心这不是问题所在:)我从DB的家伙那里复制了新的查询代码,这也包括在内。我将删除第二个。尝试绑定结果并使用prepared语句,而不是“while($row=$rs->fetch_assoc())“use”while($rs->fetch())”Ivan:你的意思是现在就改成$rs->fetch(),还是我需要将这段代码移到html端?我是个新手,所以不确定绑定结果是什么意思…谢谢-但是,当我在HTML中添加foreach语句时-我没有得到任何结果。这就是我一直被卡住的地方,我不明白。有什么想法吗?如果你的foreach语句没有返回任何东西,那就意味着你的数组是空的,或者你的foreach所在的代码块根本没有执行。你知道为什么我会在html周围添加foreach代码之前得到一条记录,但之后什么都没有吗?对不起,我不知道该怎么办。啊!这是有道理的!我已经删除了它,现在我有了所有显示的记录-但是它们在页面的顶部,而不是它们所属的地方。。。进步。我将添加一个更新的注释和修改后的代码。谢谢没问题!网格位于页面顶部,因为您可能在打印视图之前打印网格,您应该通过添加用于显示网格数据的方法来更新视图,并在$template_content object:)上使用该方法进行打印
  <?php
      foreach ($allRows as $row) {
      ?>
     <!-- HTML -->
      <?php  }  ?>
  $template_content->set('eventID',$row['EventID']);
  $template_content->set('dateOfEvent',$row['DateOfEvent']);
  $template_content->set('clientFollow',$row['ClientFollowUp']);
  $template_content->set('performerFollow',$row['PerformerFollowUp']);