我有DELETE查询,我想返回执行结果。我该怎么办?
我的PHP代码:
Function delete_cat($category){
$stmt = $mysqli->prepare("DELETE category, products FROM category INNER JOIN products WHERE category.id = ? AND product_cat = category.category_name ");
$stmt->bind_param($category);
$stmt->execute();
$stmt->close();
//return should be here
}
答案 0 :(得分:4)
所以class ToolInputViewSet(viewsets.ModelViewSet):
'''
Describes a Tool input.
'''
lookup_field = 'id'
serializer_class = StandaloneToolInputSerializer
def get_queryset(self):
tool_id = self.kwargs['tool_id']
return ToolInput.objects.filter(tool_id=tool_id)
def create(self, request, *args, **kwargs):
request.data["tool"] = self.kwargs['tool_id']
return super(ToolInputViewSet, self).create(request, *args, **kwargs)
def update(self, request, *args, **kwargs):
request.data["tool"] = self.kwargs['tool_id']
return super(ToolInputViewSet, self).update(request, *args, **kwargs)
=>返回一个bool值,无论你的查询是否成功执行,你都可以这样做:
$stmt->execute();
如果您需要获取已删除元素的计数,可以使用mysqli_stmt_affected_rows,如下所示:
function delete_cat($category){
$stmt = $mysqli->prepare("DELETE category, products FROM category INNER JOIN products WHERE category.id = ? AND product_cat = category.category_name ");
$stmt->bind_param($category);
$result = $stmt->execute();
$stmt->close();
return $result;
}
答案 1 :(得分:1)
function delete_cat($category){
$stmt = $mysqli->prepare("DELETE category, products FROM category INNER JOIN products WHERE category.id = ? AND product_cat = category.category_name ");
$stmt->bind_param($category);
$result = $stmt->execute(); // store the return of the method into a variable here so we can return it later
$stmt->close();
return $result // would return if the rows have been deleted or not.
return $mysqli->affected_rows; // would return the amount of rows deleted
}
如果要获取已删除的行数,请使用affected_rows
,如果要在SQL查询成功执行时获取结果,只需存储$stmt->execute
的返回值变成一个变量然后返回。