{"id":145,"date":"2026-05-20T15:30:36","date_gmt":"2026-05-20T15:30:36","guid":{"rendered":"https:\/\/gigz.pk\/php\/?post_type=lesson&#038;p=145"},"modified":"2026-05-21T14:41:53","modified_gmt":"2026-05-21T14:41:53","slug":"prepared-statements","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/php\/?lesson=prepared-statements","title":{"rendered":"Prepared Statements"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Prepared Statements are a secure and efficient way to execute SQL queries in PHP. They help protect web applications from SQL Injection attacks and improve database performance when executing repeated queries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Prepared Statements separate SQL code from user input, making database operations safer and more reliable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Objectives<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this training, you will be able to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Understand what Prepared Statements are<\/li>\n\n\n\n<li>Learn why Prepared Statements are important<\/li>\n\n\n\n<li>Create secure database queries in PHP<\/li>\n\n\n\n<li>Insert, update, delete, and retrieve data safely<\/li>\n\n\n\n<li>Prevent SQL Injection attacks<\/li>\n\n\n\n<li>Use MySQLi Prepared Statements in PHP<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What are Prepared Statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A Prepared Statement is a precompiled SQL query where placeholders are used instead of direct user input.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The database prepares the SQL query first, and then the values are added separately.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This process improves:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Security<\/li>\n\n\n\n<li>Performance<\/li>\n\n\n\n<li>Code readability<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Prepared Statements<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Prevent SQL Injection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prepared Statements protect databases from malicious SQL code entered by users.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Improve Performance<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Queries executed multiple times are processed faster because the SQL statement is compiled once.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Better Data Handling<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prepared Statements automatically handle special characters and data types correctly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Database Connection in PHP<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$conn = mysqli_connect(\"localhost\", \"root\", \"\", \"training_db\");<br><br>if (!$conn) {<br>    die(\"Connection failed\");<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Inserting Data Using Prepared Statements<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$name = \"Ali\";<br>$email = \"ali@example.com\";<br><br>$stmt = $conn-&gt;prepare(\"INSERT INTO users (name, email) VALUES (?, ?)\");<br><br>$stmt-&gt;bind_param(\"ss\", $name, $email);<br><br>$stmt-&gt;execute();<br><br>echo \"Data inserted successfully\";<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding the Code<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">prepare()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The prepare() method creates a prepared SQL statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$stmt = $conn-&gt;prepare(\"INSERT INTO users (name, email) VALUES (?, ?)\");<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Question marks act as placeholders for values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">bind_param()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The bind_param() method binds variables to the placeholders.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$stmt-&gt;bind_param(\"ss\", $name, $email);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first parameter defines data types.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>s = string<\/li>\n\n\n\n<li>i = integer<\/li>\n\n\n\n<li>d = double<\/li>\n\n\n\n<li>b = blob<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">execute()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The execute() method runs the SQL query.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$stmt-&gt;execute();<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Selecting Data Using Prepared Statements<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$id = 1;<br><br>$stmt = $conn-&gt;prepare(\"SELECT * FROM users WHERE id = ?\");<br><br>$stmt-&gt;bind_param(\"i\", $id);<br><br>$stmt-&gt;execute();<br><br>$result = $stmt-&gt;get_result();<br><br>while ($row = $result-&gt;fetch_assoc()) {<br>    echo $row&#91;'name'];<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Updating Data Using Prepared Statements<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$name = \"Ahmed\";<br>$id = 1;<br><br>$stmt = $conn-&gt;prepare(\"UPDATE users SET name = ? WHERE id = ?\");<br><br>$stmt-&gt;bind_param(\"si\", $name, $id);<br><br>$stmt-&gt;execute();<br><br>echo \"Record updated successfully\";<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Deleting Data Using Prepared Statements<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$id = 2;<br><br>$stmt = $conn-&gt;prepare(\"DELETE FROM users WHERE id = ?\");<br><br>$stmt-&gt;bind_param(\"i\", $id);<br><br>$stmt-&gt;execute();<br><br>echo \"Record deleted successfully\";<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Benefits of Prepared Statements<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Increased database security<\/li>\n\n\n\n<li>Protection against SQL Injection<\/li>\n\n\n\n<li>Faster repeated query execution<\/li>\n\n\n\n<li>Cleaner and more organized code<\/li>\n\n\n\n<li>Better handling of user input<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes to Avoid<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Forgetting to bind parameters<\/li>\n\n\n\n<li>Using incorrect data types<\/li>\n\n\n\n<li>Not checking database connection errors<\/li>\n\n\n\n<li>Executing queries without validation<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Always use Prepared Statements for user input<\/li>\n\n\n\n<li>Validate and sanitize data before processing<\/li>\n\n\n\n<li>Close statements after execution<\/li>\n\n\n\n<li>Use error handling for database operations<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Closing Prepared Statements<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$stmt-&gt;close();<br>$conn-&gt;close();<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Real World Applications<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Prepared Statements are commonly used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Login systems<\/li>\n\n\n\n<li>Registration forms<\/li>\n\n\n\n<li>E-commerce websites<\/li>\n\n\n\n<li>Student management systems<\/li>\n\n\n\n<li>Banking applications<\/li>\n\n\n\n<li>Online booking systems<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Prepared Statements are an essential part of secure PHP development. They improve security, performance, and reliability when working with databases. Every modern PHP application should use Prepared Statements to safely manage user data and database interactions.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/php\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Advanced PHP > MySQL Database > Prepared Statements<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779291015797\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":47,"template":"","class_list":["post-145","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Prepared Statements - Learn PHP with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn PHP Prepared Statements to secure MySQL queries, prevent SQL injection, and build safe web applications.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/php\/?lesson=prepared-statements\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Prepared Statements - Learn PHP with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn PHP Prepared Statements to secure MySQL queries, prevent SQL injection, and build safe web applications.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/php\/?lesson=prepared-statements\" \/>\n<meta property=\"og:site_name\" content=\"Learn PHP with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-21T14:41:53+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=prepared-statements\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=prepared-statements\",\"name\":\"Prepared Statements - Learn PHP with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\"},\"datePublished\":\"2026-05-20T15:30:36+00:00\",\"dateModified\":\"2026-05-21T14:41:53+00:00\",\"description\":\"Learn PHP Prepared Statements to secure MySQL queries, prevent SQL injection, and build safe web applications.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=prepared-statements#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=prepared-statements\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=prepared-statements#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/php\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced PHP > MySQL Database > Prepared Statements\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/\",\"name\":\"Learn PHP with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Prepared Statements - Learn PHP with GiGz.PK","description":"Learn PHP Prepared Statements to secure MySQL queries, prevent SQL injection, and build safe web applications.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/php\/?lesson=prepared-statements","og_locale":"en_US","og_type":"article","og_title":"Prepared Statements - Learn PHP with GiGz.PK","og_description":"Learn PHP Prepared Statements to secure MySQL queries, prevent SQL injection, and build safe web applications.","og_url":"https:\/\/gigz.pk\/php\/?lesson=prepared-statements","og_site_name":"Learn PHP with GiGz.PK","article_modified_time":"2026-05-21T14:41:53+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/php\/?lesson=prepared-statements","url":"https:\/\/gigz.pk\/php\/?lesson=prepared-statements","name":"Prepared Statements - Learn PHP with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/php\/#website"},"datePublished":"2026-05-20T15:30:36+00:00","dateModified":"2026-05-21T14:41:53+00:00","description":"Learn PHP Prepared Statements to secure MySQL queries, prevent SQL injection, and build safe web applications.","breadcrumb":{"@id":"https:\/\/gigz.pk\/php\/?lesson=prepared-statements#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/php\/?lesson=prepared-statements"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/php\/?lesson=prepared-statements#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/php"},{"@type":"ListItem","position":2,"name":"Advanced PHP > MySQL Database > Prepared Statements"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/php\/#website","url":"https:\/\/gigz.pk\/php\/","name":"Learn PHP with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson\/145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}