← Back to PHP Course | Chapter 11: Database | Lesson 6 of 21

PHP Stored Procedures

एक stored procedure database के अंदर ही saved एक recipe जैसा है। PHP बस इसका नाम कहता है और database आपके लिए सारे steps चलाता है।
Syntax
php
$stmt = $pdo->prepare("CALL procedure_name(?, ?)");
$stmt->execute([$argument1, $argument2]);

Stored Procedures का Introduction

एक stored procedure SQL logic का एक named block है जो database के अंदर ही saved है, जिसे आप हर request पर पूरा SQL text भेजने के बजाय PHP से नाम से call कर सकते हैं।

उदाहरण: Introduction to Stored Procedures

php
<?php
// Stored procedures live in the database itself, e.g. MySQL:
// CREATE PROCEDURE GetUser(IN userId INT) BEGIN SELECT * FROM users WHERE id = userId; END
echo "A named block of SQL logic saved inside the database";
?>

Stored Procedures Call करना

PHP से एक stored procedure call करना आमतौर पर एक CALL procedure_name(?, ?) statement prepare और execute करने जैसा दिखता है, parameters उसी तरह pass करते हुए जैसे आप किसी दूसरे prepared statement के लिए करेंगे।

उदाहरण: Calling Stored Procedures

php
<?php
// $stmt = $pdo->prepare("CALL GetUser(?)");
// $stmt->execute([5]);
echo "Calling a stored procedure looks like any other prepared statement";
?>

IN Parameters Pass करना

Stored procedures multi-step logic (जैसे validate करना और फिर कई tables में related rows insert करना) को एक single atomic unit की तरह encapsulate कर सकते हैं, PHP और database के बीच round-trips की संख्या कम करते हुए।

उदाहरण: Passing IN Parameters

php
<?php
// CALL CreateOrder(:userId, :productId) -- validates and inserts across multiple tables in one call
echo "One round-trip instead of several separate queries";
?>

OUT Parameters Fetch करना

क्योंकि logic आपकी PHP codebase के बजाय database में रहता है, stored procedures को आपके application code के साथ version-control और test करना ज़्यादा मुश्किल है, जो उनके performance benefits के against एक real tradeoff है।

उदाहरण: Fetching OUT Parameters

php
<?php
// Stored procedure logic lives in the database, separate from your PHP source files
echo "Harder to version-control alongside your application code";
?>

कई Result Sets Fetch करना

Modern PHP applications application code में business logic रखने और stored procedures को ज़्यादा sparingly इस्तेमाल करने को prefer करते हैं, उन्हें एक clear, specific performance या data-integrity need वाले cases के लिए reserve करते हुए।

उदाहरण: Fetching Multiple Result Sets

php
<?php
// Most app logic stays in PHP; stored procedures are reserved for specific performance needs
echo "Use sparingly, for a clear, specific reason";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. अगली call से पहले results fetch या free करना भूल जाना।
  2. OUT parameters के लिए गलत parameter mode इस्तेमाल करना।
  3. user input concatenate करके CALL statement बनाना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.