Count 总是返回 1...但它存在吗? [英] Count always returns 1...but does it exist?

查看:77
本文介绍了Count 总是返回 1...但它存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建文件之前,我正在尝试检查特定类别的评论"列中是否已存在文件名.如果它已经存在,我想将今天的日期添加到名称中以使其成为唯一的文件名.我似乎无法使用计数找到它是否存在.当我 echo $checkfile 时,它​​总是返回 1,无论文件是否存在.

I'm trying to check if a file name already exists in the "reviews" column within a particular category, before it's created. If it already exists, I want to add today's date to the name to make it a unique filename. I can't seem to find if it exists using count. When I echo $checkfile, it always returns 1, whether the file exists or not.

$today = date("Y-m-d");
$list = service_category;
$php_file_name = $last."_".$first.".php";

$check = mysqli_query($sqli, "SELECT count(reviews) FROM table WHERE `category`='$list' AND `reviews`='$php_file_name'");

$checkfile = mysqli_num_rows($check);
    if($checkfile >= 1){
        $php_file_name = $last."_".$first.$today.".php";
        }

推荐答案

当您在查询中使用 COUNT() 时,您将始终返回一行,即使只是告诉您计数为零.您需要检查 COUNT(reviews) 的值以获取该值:

When you use COUNT() in your query you will always get one row returned even if only to tell you the count is zero. You need to check the value of COUNT(reviews) to get that value:

$result = mysqli_query($sqli, "SELECT count(reviews) AS `count` FROM myTable WHERE `category`='$list' AND `reviews`='$php_file_name'");
$check = mysqli_fetch_assoc($result);

if($check['count'] >= 1){

您会注意到我给了 count(reviews) 一个别名,因为这样可以更轻松地在 PHP 代码中访问该值.

You'll notice that I gave count(reviews) an alias as it makes accessing that value easier in the PHP code.

这篇关于Count 总是返回 1...但它存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆