[英]Count MySql database rows with field value = '1'
I'm trying to count the number of rows in my database with the field 'Level' equal to '1'. 我正在尝试计算数据库中“级别”等于“ 1”的行数。 I already have this set up to count the fields, see code below.
我已经进行了设置以计算字段,请参见下面的代码。
$result = mysql_query("select count(1) FROM username");
$row = mysql_fetch_array($result);
$total = $row[0];
Now I would like to adapt this to only select field where the Level = 1 in the database. 现在,我想将其调整为仅选择数据库中Level = 1的字段。
I already have the database connection setup and its working fine. 我已经有了数据库连接设置,并且可以正常工作。
I did try this code but was gettign nowhere. 我确实尝试过此代码,但无所适从。
$admins = mysql_query("select count(1) FROM WHERE Level='1'");
$totaladmins = mysql_fetch_array($admins);
$totaladmins = $admins[0];
Once the number of rows have been calculated it is displayed to the users through the following code 一旦计算了行数,它就会通过以下代码显示给用户
<h4><?php echo $total?> Users registered</h4>
Any help is supper appreciated. 任何帮助都值得赞赏。
There problem you're having is just because you forgot to include which table to select from. 您遇到的问题仅仅是因为您忘记包含要选择的表。 Instead of:
代替:
SELECT count(1) FROM WHERE Level='1';
Your query should be: 您的查询应为:
SELECT count(1) FROM username WHERE Level='1';
Hopefully this fixes your problem. 希望这可以解决您的问题。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.