dousu8456
dousu8456
2016-05-30 14:16

返回Mysqli execute()的结果

已采纳

I have a DELETE query, and I want to return the result of the execution. How can I do it ?

My php code:

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

}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duangangmo0583 duangangmo0583 5年前

    So $stmt->execute(); => returns a bool value, whether your query was successfully executed or not, and you can do it simply like this:

    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;
    }
    

    If you needed to get the count of the deleted elements, you can use mysqli_stmt_affected_rows, something like this:

    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();
        $result = $stmt->affected_rows;
        $stmt->close();
       return $result;
    }
    
    点赞 评论 复制链接分享
  • dongsu3664 dongsu3664 5年前
    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
    
    }
    

    If you want to get the amount of rows that have been deleted you use affected_rows, if you want to just get the result if the SQL query was performed succesfull, simply store the return of $stmt->execute into a variable and return that.

    http://php.net/manual/en/mysqli-stmt.affected-rows.php

    http://php.net/manual/de/mysqli-stmt.execute.php

    点赞 评论 复制链接分享