简体   繁体   English

如果mysql_num_rows等于0则不起作用

[英]If mysql_num_rows equals to ZERO is NOT working

I'm using this code to count rows and trying to do IF num rows equals to ZERO do INSERT if not UPDATE but it doesn't work. 我正在使用此代码来计数行, IF num rows equals to ZERO do INSERT if not UPDATE ,则尝试执行IF num rows equals to ZERO do INSERT if not UPDATE但它不起作用。

When I use == operator nothing happens. 当我使用==运算符时,什么也没有发生。 If I use >= operator script inserting the values but running insert query on every refresh and values are duplicating in MySQL table. 如果我使用>=运算符脚本来插入值,但每次刷新时都运行插入查询,则在MySQL表中复制值。

Here is the code: 这是代码:

$isexist = mysql_query("select count(*) from wcddl_filehosts where downloadid = '".$download[id]."'");

if (mysql_num_rows($isexist) == 0) {
mysql_query("insert into wcddl_filehosts (downloadid, rs) values ('".$download[id]."','$totalRS')");
} else {
mysql_query("update wcddl_filehosts set rs = '$totalRS' where downloadid = '".$download[id]."'");
}

What's wrong with this? 这怎么了

Count will return a value, and you cannot count and then call mysql_num_rows. Count将返回一个值,您无法计数,然后调用mysql_num_rows。 It is either one of the other. 它是彼此之一。

You could do 你可以做

$isExist = mysql_query("Select count(id) from ..."); 
$r = mysql_fetch_array($isExist);
if($r['COUNT(id)'] > 0){
//item exists
}else{
//item doesnt exist
}

If alternatively you can do the query as: 如果可以选择执行以下查询:

$isexist = mysql_query("select * from wcddl_filehosts where downloadid = '".$download[id]."'");
if(mysql_num_rows($isExists)>0){
//we have items
}else{
//we dont have items
}

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

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