PHP Stored Procedures
In this page:
$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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
Stored Procedures Call करना
PHP से एक stored procedure call करना आमतौर पर एक CALL procedure_name(?, ?) statement prepare और execute करने जैसा दिखता है, parameters उसी तरह pass करते हुए जैसे आप किसी दूसरे prepared statement के लिए करेंगे।
उदाहरण: Calling Stored Procedures
<?php
// $stmt = $pdo->prepare("CALL GetUser(?)");
// $stmt->execute([5]);
echo "Calling a stored procedure looks like any other prepared statement";
?>
Login to try C/C++/Java/PHP code in the editor
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
// CALL CreateOrder(:userId, :productId) -- validates and inserts across multiple tables in one call
echo "One round-trip instead of several separate queries";
?>
Login to try C/C++/Java/PHP code in the editor
OUT Parameters Fetch करना
क्योंकि logic आपकी PHP codebase के बजाय database में रहता है, stored procedures को आपके application code के साथ version-control और test करना ज़्यादा मुश्किल है, जो उनके performance benefits के against एक real tradeoff है।
उदाहरण: Fetching OUT Parameters
<?php
// Stored procedure logic lives in the database, separate from your PHP source files
echo "Harder to version-control alongside your application code";
?>
Login to try C/C++/Java/PHP code in the editor
कई 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
// Most app logic stays in PHP; stored procedures are reserved for specific performance needs
echo "Use sparingly, for a clear, specific reason";
?>
Login to try C/C++/Java/PHP code in the editor
- अगली call से पहले results fetch या free करना भूल जाना।
- OUT parameters के लिए गलत parameter mode इस्तेमाल करना।
- user input concatenate करके CALL statement बनाना।
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- PHP MySQL Introduction
- PHP MySQLi Connection
- PHP PDO Introduction
- PHP CRUD Operations
- PHP Prepared Statements
- PHP Stored Procedures
- PHP Transactions
- PHP Error Handling in DB
- PHP MySQL Connect
- PHP MySQL Create DB
- PHP MySQL Create Table
- PHP MySQL Insert Data
- PHP MySQL Get Last ID
- PHP MySQL Insert Multiple
- PHP MySQL Prepared Statements
- PHP MySQL Select Data
- PHP MySQL Where
- PHP MySQL Order By
- PHP MySQL Delete Data
- PHP MySQL Update Data
- PHP MySQL Limit Data