duanrong6802 2016-05-17 07:27
浏览 10
已采纳

在表中显示mysql_fetch_assoc的结果

I'm using the function mysql_fetch_assoc to return the data in database, but I don't know how to print its result in a table. Here is the code that does the process.

$number_of_date= "SELECT SUM(number_of_date) AS number_of_date FROM emp_leaves WHERE  emp_id='$userID' AND leave_category='Annual' AND apply_year='$year'";
$res_qry = mysql_query($number_of_date) or die ('Invalid query :: <br/>'.$number_of_date.' <br/>'.mysql_error());

$rowqry = mysql_fetch_assoc($res_qry);
$number_date = $rowqry ['number_of_date'];
<tr>
    <?php while($rowqry = mysql_fetch_assoc($res_qry)) { ?>
        <td><?php echo $rowqry ['number_of_date']?></td>
    <?php } } ?>
</tr>

It does not work. What is the correct way to display this result ?

  • 写回答

5条回答 默认 最新

  • doupoji3856 2016-05-17 07:34
    关注

    By using mysql_fetch_assoc() twice, you assign the first row of result to $rowqry already. In your query, you will only have a single row of result because of selecting the SUM() of all the results, so that leaves no more results in the while loop().

    You can just remove your while loop() because it is unnecessary as you will only get a single row.

    Also, the fact that your provided code, an HTML entity is inside a PHP. Use echo:

    $number_of_date= "SELECT SUM(number_of_date) AS number_of_date FROM emp_leaves WHERE  emp_id='$userID' AND leave_category='Annual' AND apply_year='$year'";
    $res_qry = mysql_query($number_of_date) or die ('Invalid query :: <br/>'.$number_of_date.' <br/>'.mysql_error());
    
    $rowqry = mysql_fetch_assoc($res_qry);
    $number_date = $rowqry['number_of_date'];
    
    echo '<tr>
              <td>'.$number_date.'</td>
          </tr>';
    

    Note: Use mysqli_* extension instead of deprecated mysql_*.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?