简体   繁体   English

空的PHP计数从mysql_query返回1而不是0

[英]php count returning 1 not 0 from mysql_query when empty

I am trying to get a count and I am getting 1 instead of 0 from it. 我试图得到一个计数,我从中得到1而不是0。 I have looked thoroughly though the web and this site. 我已经仔细地浏览了网站和本网站。 I have even been trying to figure it out on my own for a long time. 很长时间以来,我甚至一直试图自己解决这个问题。 I keep coming empty handed here. 我一直空着手过来。

So Basically what I am trying to do is make a like system for my users. 因此,基本上我想做的就是为我的用户创建一个类似的系统。 I can get everything to work correctly the count works except for one thing. 我可以使所有工作正常进行,除一件事外,其他所有工作都可以进行。 When they have liked it it returns 1 not 0 which it should be. 当他们喜欢它时,它返回1而不是0,应该是0。

Here is my code for the count. 这是我的计数代码。 I am not posting all the coding for security reasons and it really doesn't need to since its about the counting part not the rest. 由于安全原因,我并没有发布所有编码,而实际上它并不需要,因为它是关于计数部分而不是其余部分。

$sql_like = mysql_query("SELECT * FROM posts WHERE mem2_id='$id' ORDER BY post_date DESC LIMIT 40");

while($row = mysql_fetch_array($sql_like)){

$like1 = $row['like_array'];
$like3 = explode(",", $like1);
    $likeCount = count($like3);


}

So here is the code that determines the number. 因此,这是确定数字的代码。 Any ideas what is wrong with this? 任何想法这有什么问题吗? Why its returning 1 not 0 when the item is empty? 当项目为空时为什么返回1而不是0?

Calling explode on an empty string gives an array containing the empty string. 在空字符串上调用explode得到一个包含空字符串的数组。 This is one element, not zero. 这是一个元素,而不是零。

I would suggest that you change your database design if possible so that you don't store the values separated by commas. 我建议您尽可能更改数据库设计,以免存储用逗号分隔的值。 Use a separate table instead. 请改用单独的表。

If you can't change the database design you can handle the empty string separately. 如果您无法更改数据库设计,则可以单独处理空字符串。

// you do escape your id right??? (sql injection prevention)
$sql_like = mysql_query("SELECT * FROM posts WHERE mem2_id='$id' ORDER BY post_date DESC LIMIT 40");

while($row = mysql_fetch_array($sql_like)){
    $likeCount = 0;
    $like1 = trim($row['like_array']);
    if ($like1) {
        $like3 = explode(",", $like1); // exploding emtpy string would result in array('')
        $likeCount = count($like3);
    }
}

count() returns the number of indexes in an array or something in an object ( PHP: count - Manual ). count()返回数组或对象中某些内容的索引数( PHP:count-Manual )。

if a string var is used rather than an array or object it returns 1. it has to get a null value in order to return 0. 如果使用字符串var而不是数组或对象,则它返回1。它必须获取null值才能返回0。

you can give it a go by trying: 您可以尝试以下方法:

print count("");

and

print count(null);

You'll have better luck if you use explode() to break the sql output into an array and then run a check with an if statement. 如果使用explode()将sql输出拆分为一个数组,然后使用if语句运行检查,那么会更好。 Something like the following: 类似于以下内容:

$like3 = explode(',',$like1);
if (count($like1)=1 && $like1[0] == '')
        // etc ..

I hope this helps 我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM