dozan0001 2013-09-12 07:18
浏览 51
已采纳

嵌套While While with mysql_fetch_array给出空白结果

I have used following code, Please solve any error if exist

I am trying to catch Category name from category table using parentID from Subcategory table

here is the code

$query_select = "SELECT * FROM subcategory where parentID<>0 and isDisabled=0 and isDeleted=0";
$result_select = mysql_query($query_select) or die(mysql_error());
$rows = array();
while($row = mysql_fetch_array($result_select))
  $rows[] = $row;

foreach($rows as $row){ 
  $id = $row['subcatID'];
  $subname = $row['subcatName'];
  $parentID= $row['parentID'];
  $status = $row['isDisabled'];
  $catname="";
  echo "<tr><td style='display:none;'>$id</td>";
  echo "<td><center>$subname</center></td>";
  $resultnew=mysql_query("select catName from category where catID=$parentID");

  while(mysql_fetch_row($resultnew)){
    $catname = $resultnew[0];
    echo "<script>alert($catname);</script>";
    echo "<td class='center'><center>$catname</center></td>";
  }
  if($status==0)
    {
      echo "<td class='center'><center><span class='label label-success'>Active</span></center></td>";
    }else{
    echo "<td class='center'><center><span class='label label-important'>Disabled</span></center></td>";
  }
  echo "<td class='center'><center>";
  echo "<a class='btn btn-success' href='#'><i class='icon-zoom-in icon-white'></i>View</a>";
  echo "<a class='btn btn-info' href='#'><i class='icon-edit icon-white'></i>Edit</a>";
  echo "<a class='btn btn-danger' href='#'><i class='icon-trash icon-white'></i>Delete</a>";
  echo "</center></td>";
  echo "</tr>";
}
  • 写回答

1条回答 默认 最新

  • drnzpd579935 2013-09-12 07:42
    关注

    Change:

      while(mysql_fetch_row($resultnew)){
        $catname = $resultnew[0];
        echo "<script>alert($catname);</script>";
        echo "<td class='center'><center>select catName from category where catID=$parentID $catname</center></td>";
      }
    

    to:

      while($newrow = mysql_fetch_row($resultnew)){
        $catname = $newrow[0];
        echo "<script>alert($catname);</script>";
        echo "<td class='center'><center>select catName from category where catID=$parentID $catname</center></td>";
      }
    

    However, I presume catID is a unique key, so this query can only return one row, so there's no need for a while loop at all. Just write:

    if ($newrow = mysql_fetch_row($resultnew)) {
        $catname = $newrow[0];
        echo "<script>alert($catname);</script>";
        echo "<td class='center'><center>select catName from category where catID=$parentID $catname</center></td>";
      }
    

    But you can also combine this with your main query, so you don't need to do multiple queries:

    SELECT s.subcatID, s.subcatName, s.parentID, s.isDisabled, c.catName
    FROM subcategory s
    LEFT JOIN category c ON c.catID = s.parentID
    WHERE s.parentID <> 0 and s.isDisabled = 0 and s.isDeleted = 0
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图