-3

可能重复:
警告:mysql_fetch_* 期望参数 1 是资源,布尔给定错误

这有什么问题?前 2 个选择工作正常,其他 2 个返回此错误:

警告:mysql_fetch_assoc() 期望参数 1 是资源,给定的布尔值

<?php 
$result=mysql_query("SELECT count(*) as total from urls WHERE username = '$username' AND approved = 'yes' ");
  $data=mysql_fetch_assoc($result);
echo $data['total'];?></td>

 <td><?php 
$result2=mysql_query("SELECT count(*) as total2 from urls WHERE username = '$username' AND approved = 'no' ");
  $data2=mysql_fetch_assoc($result2);
echo $data2['total2'];?></td>

  <td><?php 
$result3=mysql_query("SELECT count(*) as total3 from buy WHERE username = '$username' AND status = 'active' ");
  $data=mysql_fetch_assoc($result3);
echo $data['total3'];?></td>

  <td><?php 
$result4=mysql_query("SELECT count(*) as total4 from buy WHERE username = '$username' AND status = 'complete' ");
 $data=mysql_fetch_assoc($result4);
echo $data['total4'];?></td>
4

1 回答 1

0

您可以在一个查询中执行此操作,如下所示:

SELECT
  SUM(CASE WHEN approved = 'yes'      THEN 1 ELSE 0 END) AS Total1,
  SUM(CASE WHEN approved = 'no'       THEN 1 ELSE 0 END) AS Total2,
  SUM(CASE WHEN status   = 'active'   THEN 1 ELSE 0 END) AS Total3,
  SUM(CASE WHEN status   = 'complete' THEN 1 ELSE 0 END) AS Total4
FROM urls 
WHERE username = '$username';
于 2012-12-28T16:11:38.640 回答