mysqli_prepare 
delete from table where id in (1,3) 这样可以执行
如果把in里面的1,3 做参数只执行1
类似:delete from table where id in (?) bind_param("s","1,3")这样就只有1被删除,3还存在mysqli

解决方案 »

  1.   

    function paramtypez($val)
    {
                $types = '';                        //initial sting with types
                foreach($val as $para) 
                {     
                    if(is_int($para)) {
                        $types .= 'i';              //integer
                    } elseif (is_float($para)) {
                        $types .= 'd';              //double
                    } elseif (is_string($para)) {
                        $types .= 's';              //string
                    } else {
                        $types .= 'b';              //blob and unknown
                    }
                }
                return $types;
    }

    function refValues($arr){
        if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
        {
            $refs = array();
            foreach($arr as $key => $value)
                $refs[$key] = &$arr[$key];
            return $refs;
        }
        return $arr;
    }define('HOST', 'localhost');
    define('USER', 'root');
    define('PASS', '');
    define('DBNAME', 'test');$db = new mysqli(HOST, USER, PASS, DBNAME); 
    $ids    = array( 10,11);
    $params = implode(",", array_fill(0, count($ids), "?"));
    $sql    = "DELETE FROM tt WHERE id in($params)";
    $stmt   = $db->prepare($sql);
    array_unshift($ids,paramtypez($ids));
    call_user_func_array(array($stmt, 'bind_param'), refValues($ids));  //php 5.3+, 5.3 以下去掉refValues函数
    $stmt->execute();$row_affected = $db->affected_rows;
    echo $row_affected;
    $db->close();
      

  2.   

    补充一下
    call_user_func_array(array($stmt, 'bind_param'), refValues($ids));5.3以下时这句不用去掉refValues函数,因为函数里面已经加了判断。