首页 >社区问答列表 >如果把mysqli_query 封装还有没有办法使用mysqli_insert_id

如果把mysqli_query 封装还有没有办法使用mysqli_insert_id

public static function mquery($sql){

$conn = self::db_connect();

$rs=mysqli_query($conn,$sql);

mysqli_close($conn);

public static function insert($table,$data){

$conn=self::db_connect();

$fields = array();

$values = array();

foreach ($data as $key => $value) {

$value = str_replace("'", '&apos', $value);//单引号替换字符实体

$value = str_replace('"', '&quot', $value);//双引号替换

$fields[]= "`".$key."`";

$values[]="'".$value."'";


}

$fields = implode(',', $fields);

$values = implode(',',$values);

//sql 语句

$sql = "insert into {$table} ({$fields}) values ({$values})";

//dump($sql);exit();

$insert_id =0;

if (mysqli_query( $conn,$sql)){

$insert_id = mysqli_insert_id($conn);

}

mysqli_close($conn);

return $insert_id;

}

return $rs;

}