可以用query执行下面的sql语句吗?$sql="";
for($i=0;$i<count($array);$i++){
$sql.="update v_item set i_count=i_count+1 where i_id=".$array[$i].";";
}
$DB->query($sql);

解决方案 »

  1.   

    如果你数据库是mssql或者数据库扩展是mysqli的话,这样是可以的,mysql系的不行。
      

  2.   

    你的方法是错的,我给两个方法给你,你试一下,具体哪个效率高,你自己测试一下看:方法一:
    foreach($array as $key =>$value){$DB->query("update v_item set i_count=i_count+1 where i_id=".$array[$key]."");}
    方法二:foreach($array as $key => $value){
    $inwhere.=$dou.$value;
    $dou=",";
    }
    $DB->query("update v_item set i_count=i_count+1 where i_id in (".$inwhere.")");
      

  3.   

    另外,
    for($i=0;$i<count($array);$i++){
    //执行程序
    }以后你不要像上面这样写循环,你或许可以像下满面这样写
    $counts=count($array);
    for($i=0;$i<$counts;$i++){
    //执行程序
    }
    你的写法是每循环一次就要去count一下数组,而我的写法是开始的时候只count一次数据就行,可以想想哪一种效率要高。
    这就是优化,要养成习惯。