使用PHP删除select中所有n级别的类别

function delete_subcats($id) {
    global $con;
    $sql = "select * from `add_category` where `id` = ".$id;
    $res = mysqli_query($con,$sql);
    if(mysqli_num_row($res)>0){
        while($row = mysqli_fetch_assoc($res)){
            delete_subcats($row['id']);
        }
    }

    mysqli_query($con,"delete from `add_category` where `id` = ".$id);
}

you can give foreign key of parent id than after fire simple delete query.

OR

you can use recursive function.

function delete_category($cat_id, $table = "nested_category") {
        $query = $this->db->select('id,pid')
                        ->from($table)
                        ->where('pid', $cat_id)
                        ->get()->result();
        foreach ($query as $row)
            $this->delete_category($row->id);
        $this->db->delete($table, array("id" => $cat_id));
    }

OR

function delete_subcats($id) {
    global $con;
    $sql = "select * from `add_category` where `pid` = ".$id;
    $res = mysqli_query($con,$sql);
    if(!empty($res)){
        while($row = mysqli_fetch_assoc($res)){
            delete_subcats($row['id']);
        }
    }

    mysqli_query($con,"delete from `add_category` where `id` = ".$id);
}