Home > Database > Mysql Tutorial > How Do You Create and Execute Stored Procedures in phpMyAdmin?

How Do You Create and Execute Stored Procedures in phpMyAdmin?

Linda Hamilton
Release: 2024-11-04 06:23:01
Original
1000 people have browsed it

How Do You Create and Execute Stored Procedures in phpMyAdmin?

How to Create and Execute Stored Procedures in phpMyAdmin

Creating and executing stored procedures in phpMyAdmin is a simple process that can enhance your database management capabilities. Here's a step-by-step guide to help you get started:

Creating a Stored Procedure

  1. Navigate to your database in phpMyAdmin.
  2. Click on the 'Routines' tab located in the header.
  3. Click on 'Add routine'.
  4. In the popup window that appears, type in your stored procedure code.
  5. Click on "GO" to create the stored procedure.

For Example:

CREATE PROCEDURE calculate_age(id VARCHAR(10))
BEGIN
    SELECT name, (DATE(NOW()) - DATE_OF_BIRTH)/365 AS age
    FROM people
    WHERE id = id;
END
Copy after login

Calling a Stored Procedure Using MVC Architecture

In an MVC architecture, you can call a stored procedure from your controller. Here's a simplified example using PHP and mysqli:

<code class="php">// Get database connection
$db = new mysqli('127.0.0.1:3306', 'username', 'password', 'database');

// Prepare statement to call stored procedure
$stmt = $db->prepare("CALL get_sales(?)");

// Bind parameter to stored procedure
$stmt->bind_param('i', $order_id);

// Execute stored procedure
$stmt->execute();

// Get result from stored procedure
$result = $stmt->get_result();</code>
Copy after login

By following these steps, you can easily create and execute stored procedures in phpMyAdmin and leverage them in your MVC applications to perform complex database operations efficiently.

The above is the detailed content of How Do You Create and Execute Stored Procedures in phpMyAdmin?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template