BIG

Call stored procedure sample code using PHP

 

#MariaDB console

# Create stored procedure

DELIMITER $$
CREATE PROCEDURE GetProductById(IN product_id INT)
BEGIN
    SELECT * FROM products WHERE id = product_id;
END $$
DELIMITER ;

# Call stored procedure

CALL GetProductById(5);

 

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$product_id = 5;
$stmt = $conn->prepare("CALL GetProductById(?)");
$stmt->bind_param("i", $product_id);
$stmt->execute();

$result = $stmt->get_result();

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "No results";
}

$stmt->close();
$conn->close();

?>
LIST