smile 22121 smile 22121 - 6 months ago 20
PHP Question

Return the result of 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

}

Answer

So $stmt->execute(); => return bool value, whether your query was made OK or with errors. If you need just this, simply make

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 need count of deleted elements , use mysqli_stmt_affected_rows kind of 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;
}