0

我有一个值为 1 或 0 的已发布字段。我希望能够提醒用户有多少记录需要发布或未发布。我想计算表推荐中published等于的记录数0并将该值输出为

Awaiting to be published:#with published=0

我有这个,我意识到这是错误的:

$sql = mysql_query("SELECT * FROM testimonials WHERE published='0'");
$result=mysql_num_rows($sql);
echo $result;
4

3 回答 3

2
$sql = mysql_query("SELECT SUM(published=0) AS nb_unpublished,
                           SUM(published=1) AS nb_published FROM testimonials");
$result = mysql_fetch_assoc($sql);
echo "Records awaiting to be published:" . $result['nb_unpublished'] . '<br>';
echo "Records already published:" . $result['nb_published'] . '<br>';
于 2012-07-19T00:58:24.140 回答
0

这是正确的。您还可以找到这样的记录计数:

$sql = mysql_query("SELECT COUNT(*) AS `rows` FROM `testimonials` WHERE `published`='0'");
$result=mysql_fecth_array($sql);
echo "Awaiting to be published:".$result['rows'];
于 2012-07-19T01:01:02.330 回答
0

你可以使用这个:

SELECT SUM(IF(published='0',1,0)) AS 'count_awaiting',
  SUM(IF(published='1',1,0)) AS 'count_publish' 
FROM testimonials

#count_awaiting is number of records with published=0 
#count_publish is number of records with published=1
于 2012-07-19T00:59:06.323 回答