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.
When I use ==
operator nothing happens. If I use >=
operator script inserting the values but running inse开发者_如何学编程rt query on every refresh and values are duplicating in MySQL table.
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. 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
}
精彩评论