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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度