Prepared Statement Example

Prepared Statement Example - Hindi

Prepared Statement Example – अक्सर हमारी जरूरत ऐसी होती है कि हमें एक ही SQL Query को बार-बार Execute करने की जरूरत पडती है, जहां हर Iteration में हम Different Parameter को Use करते हैं। इस प्रकार की जरूरत को query() Method का प्रयोग करते हुए किसी Looping Statement में पूरा करना PHP के लिए काफी Memory व Time Consuming काम होता है। इसलिए इस प्रकार की जरूरत को पूरा करने के लिए PHP हमें Prepared Statements की सुविधा प्रदान करता है। PHP हमें दो प्रकार के Prepared Statements Provide करता हैः

  • Bound Parameters

Bound Parameters में PHP हमें MySQL Server पर किसी Query को Store करने की सुविधा देता है और हमें MySQL Server पर केवल वे ही Data Send करने होते हैं, जो कि Repeatedly Change हो रहे होते हैं।

  • Bound Results

इस तरीके में हम किसी Indexed या Associative Array को किसी Resultset से Values प्राप्त करने के लिए Use करते हैं और Resultset से प्राप्त Values को किसी PHP Variable में Store करते हैं तथा बाद में जरूरत के अनुसार Record के Fields से Associated Variables को उपयोग में लेते हैं।

Preparing the Statement for Execution

हम उपरोक्त में से चाहे जिस तरीके को Use करें, हमें सबसे पहले Execution के लिए एक Prepared Statement को Create करना होता है। इस काम को पूरा करने के लिए हमें prepare() Method को Use करना होता है। इस Method को हम निम्नानुसार Use करते हैं:

$query = “DELETE FROM subscribers WHERE userid=’1′ “;
$result = $db_conn->query($query);

// Create a statement object
$stmt = $mysqli->stmt_init();

// Prepare the statement for execution
$stmt->prepare($query);

//.. Do something with the prepared statement
// Recuperate the statement resources
$stmt->close();

// Close the connection
$db_conn->close();

Executing a Prepared Statement

एक बार prepare() Method का प्रयोग करने के बाद हमें इस Prepared Statement को Execute करना होता है। जब हम किसी Prepared Statement को Execute करते हैं, Exactly उसी समय हमें ये तय करना होता है कि हम Bound Parameters को Use करना चाहते हैं या Bound Results को।

यदि हम Bound Parameters को Use करना चाहते हैं, तो हमें Parameters को Bound करने के बाद Prepared Statement को Execute करना होता है। Parameters को Bound करने के लिए हमें bind_param() Method को Use करना होता है।

जबकि यदि हम Bound Results को Use करना चाहते हैं, तो हमें Results को Bound करने के बाद Prepared Statement को Execute करना होता है। Results को Bound करने के लिए हमें bind_result() Method को Use करना होता है।

दोनों ही स्थितियों में हमें Prepared Statement को Execute करने के लिए PHP के execute() Method को Call करना होता है।

Recuperating Prepared Statement Resource

एक बार Prepared Statement का काम समाप्त हो जाने के बाद हमें उस Prepared Statement द्वारा Occupied Resource को Release करना होता है। इस काम को करने के लिए हमें close() Method को Call करना होता है, जैसाकि हमने पिछले PHP Code Segment में किया है।

Binding Parameters

जब हम Bound Parameter Prepared Statement तरीके को Use करते हैं, तो हमें bind_param() Method को Call करके Return होने वाले Record के हर Field से एक Variable को Bind करना होता है। इस Method का Syntax निम्नानुसार होता हैः

boolean bind_param(string types, mixed &var1, mixed &varN, …)

इस Method के पहले Parameter के रूप में हमें उस Data Type को Specify करना होता है, जो कि Resultset से Return होने वाले Record के Field का Data Type होता है। इस Parameter में हम निम्न में से किसी एक को Specify कर सकते हैं:

i     :     INTEGER Type
d    :     DOUBLE and FLOAT Type
b    :     BLOB Type
s    :     All other types

निम्न उदाहरण द्वारा Prepared Statement with Bound Parameters की प्रक्रिया को बेहतर तरीके से समझा जा सकता हैः

<?php
// Create a new server connection
$db_conn = new mysqli("127.0.0.1", "registration", "pwd", "registration");

// Create the query and corresponding placeholders
$query = "INSERT INTO subscribers SET username=?, email=?, pwd=?";

// Create a statement object
$stmt = $mysqli->stmt_init();

// Prepare the statement for execution
$stmt->prepare($query);

// Bind the parameters
$stmt->bind_param($username, $email, $password);

// Assign the posted username array
$usernamearray = $_POST['username'];

// Assign the posted email array
$emailarray = $_POST['email'];

// Assign the posted password array
$passwordarray= $_POST['password'];

// Initialize the counter
$x = 0;

// Cycle through the array, and iteratively execute the query
while ($x < sizeof($usernamearray)) {
$username = $usernamearray[$x];
$email = $emailarray[$x];
$password = $passwordarray[$x];
$stmt->execute();
}

// Recuperate the statement resources
$stmt->close();

// Close the connection
$db_conn->close();
?>

उपरोक्त PHP Program किसी HTML Form से आने वाले सभी Username, Email व Passwords को एक साथ एक ही बार में Database में Store कर देगा।

Binding Results or Binding Variables

जिस तरह से हमने पिछली PHP Script में Parameters को Bind किया है उसी तरह से हम Variables को भी Bind कर सकते हैं। यदि हम इस दूसरे तरीके से पिछले PHP Script को Modify करना चाहें, तो हमारी Modified PHP Script निम्नानुसार होगीः

<?php
// Create a new server connection
$db_conn = new mysqli("127.0.0.1", "registration", "pwd", "registration");

// Create the query and corresponding placeholders
$query = "INSERT INTO subscribers SET username=?, email=?, pwd=?";

// Create a statement object
$stmt = $mysqli->stmt_init();

// Prepare the statement for execution
$stmt->prepare($query);

// Execute the statement
$stmt->execute();

// Bind the parameters
$stmt->bind_result($username, $email, $password);

// Cycle through the array, and iteratively execute the query
while ($stmt->fetch()) {
	printf("%s, %s, %s <br>", $username, $email, $password);
}

// Recuperate the statement resources
$stmt->close();

// Close the connection
$db_conn->close();
?>

ये PHP Script भी Exactly उसी तरह से काम करता है, जिस तरह से पिछला कर रहा है।

PHP Query Result Parsing
PHP HTML Functions

PHP in Hindiये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook PHP in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी रहा, तो निश्चित रूप से ये पुस्तक भी आपके लिए काफी उपयोगी साबित होगी। 

PHP in Hindi | Page: 647 | Format: PDF

BUY NOW GET DEMO REVIEWS

MidRangeHub Finance Auto Loans Bank Business Health Insurance Insurance Investment Law Lif Insurance Loan Stocks how to endorse a check chase sapphire travel insurance chase sapphire travel delay when are property taxes due Tower Loans how to sell stocks on cash app Voided Check Examples Personal Finance Books Collateral Loans how to sell stocks on cashapp how do you sell your stocks on cash app how to sell stock on cash app joint account sofi joint account ally joint account capital one joint account best bank for joint account chase joint account cyber insurance coverage silverfort free cyber insurance coverage silverfort monjouro savings card Money6x Real Estate