{"id":146,"date":"2026-05-20T15:32:43","date_gmt":"2026-05-20T15:32:43","guid":{"rendered":"https:\/\/gigz.pk\/php\/?post_type=lesson&#038;p=146"},"modified":"2026-05-21T14:42:00","modified_gmt":"2026-05-21T14:42:00","slug":"pdo-basics","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/php\/?lesson=pdo-basics","title":{"rendered":"PDO Basics"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction to PDO<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PDO stands for PHP Data Objects. It is a database access layer in PHP that provides a secure and consistent way to connect and work with databases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PDO allows developers to use the same functions for different database systems such as MySQL, PostgreSQL, SQLite, and Oracle. It is widely used because it improves security, flexibility, and code readability.<\/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 the purpose of PDO in PHP<\/li>\n\n\n\n<li>Connect PHP applications with databases using PDO<\/li>\n\n\n\n<li>Execute SQL queries safely<\/li>\n\n\n\n<li>Use prepared statements<\/li>\n\n\n\n<li>Insert, update, delete, and fetch data<\/li>\n\n\n\n<li>Handle database errors effectively<\/li>\n\n\n\n<li>Improve database security<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is PDO<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PDO is a PHP extension used for database access. It provides a simple and secure method for interacting with databases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PDO supports multiple database systems and helps prevent SQL injection attacks through prepared statements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of PDO<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Database Flexibility<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PDO supports multiple database systems using the same syntax.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Improved Security<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Prepared statements help protect applications from SQL injection.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Better Error Handling<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PDO provides exception handling for database errors.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Cleaner Code<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PDO makes database operations more organized and readable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PDO Connection Syntax<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To connect PHP with a MySQL database using PDO:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$host = \"localhost\";<br>$dbname = \"testdb\";<br>$username = \"root\";<br>$password = \"\";<br><br>try {<br>    $conn = new PDO(\"mysql:host=$host;dbname=$dbname\", $username, $password);<br><br>    $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);<br><br>    echo \"Connected Successfully\";<br>}<br>catch(PDOException $e) {<br>    echo \"Connection Failed: \" . $e-&gt;getMessage();<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding the Connection Code<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">PDO Object<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The PDO object creates a connection between PHP and the database.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Try Catch Block<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The try catch block handles connection errors safely.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Error Mode<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">PDO::ERRMODE_EXCEPTION displays database errors as exceptions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Inserting Data with PDO<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$sql = \"INSERT INTO students(name, email)<br>VALUES('Ali', 'ali@example.com')\";<br><br>$conn-&gt;exec($sql);<br><br>echo \"Data Inserted Successfully\";<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using Prepared Statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Prepared statements improve security and performance.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$stmt = $conn-&gt;prepare(\"INSERT INTO students(name, email)<br>VALUES(:name, :email)\");<br><br>$stmt-&gt;bindParam(':name', $name);<br>$stmt-&gt;bindParam(':email', $email);<br><br>$name = \"Ahmed\";<br>$email = \"ahmed@example.com\";<br><br>$stmt-&gt;execute();<br><br>echo \"Record Added\";<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Fetching Data from Database<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$stmt = $conn-&gt;prepare(\"SELECT * FROM students\");<br>$stmt-&gt;execute();<br><br>$result = $stmt-&gt;fetchAll();<br><br>foreach($result as $row) {<br>    echo $row&#91;'name'];<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Updating Data with PDO<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$sql = \"UPDATE students<br>SET email='newemail@example.com'<br>WHERE id=1\";<br><br>$conn-&gt;exec($sql);<br><br>echo \"Record Updated\";<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Deleting Data with PDO<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>$sql = \"DELETE FROM students WHERE id=1\";<br><br>$conn-&gt;exec($sql);<br><br>echo \"Record Deleted\";<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Fetch Methods in PDO<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">fetch()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Returns a single row from the result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">fetchAll()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Returns all rows from the result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">fetchColumn()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Returns a single column value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PDO Error Handling<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PDO exceptions help developers identify database issues quickly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>try {<br>    $conn = new PDO(\"mysql:host=localhost;dbname=testdb\", \"root\", \"\");<br>}<br>catch(PDOException $e) {<br>    echo $e-&gt;getMessage();<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Security Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Always use prepared statements<\/li>\n\n\n\n<li>Validate user input<\/li>\n\n\n\n<li>Avoid displaying database errors to users<\/li>\n\n\n\n<li>Use strong database passwords<\/li>\n\n\n\n<li>Keep PHP and database systems updated<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real World Uses of PDO<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">PDO is commonly used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Dynamic websites<\/li>\n\n\n\n<li>Content management systems<\/li>\n\n\n\n<li>E-commerce platforms<\/li>\n\n\n\n<li>Login and registration systems<\/li>\n\n\n\n<li>Student management systems<\/li>\n\n\n\n<li>Inventory management applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Career Opportunities<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Learning PDO can help you become:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP Developer<\/li>\n\n\n\n<li>Backend Developer<\/li>\n\n\n\n<li>Full Stack Developer<\/li>\n\n\n\n<li>Database Application Developer<\/li>\n\n\n\n<li>Web Application Developer<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Final Presentation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In your final presentation, explain:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What PDO is<\/li>\n\n\n\n<li>Advantages of PDO<\/li>\n\n\n\n<li>Database connection process<\/li>\n\n\n\n<li>Prepared statements and security<\/li>\n\n\n\n<li>CRUD operations using PDO<\/li>\n\n\n\n<li>Error handling techniques<\/li>\n\n\n\n<li>Real-world applications of PDO<\/li>\n<\/ul>\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 > PDO Basics<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779291168654\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":48,"template":"","class_list":["post-146","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>PDO Basics - Learn PHP with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn PDO basics in PHP including database connection, CRUD operations, prepared statements, and secure coding.\" \/>\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=pdo-basics\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PDO Basics - Learn PHP with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn PDO basics in PHP including database connection, CRUD operations, prepared statements, and secure coding.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/php\/?lesson=pdo-basics\" \/>\n<meta property=\"og:site_name\" content=\"Learn PHP with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-21T14:42:00+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=pdo-basics\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=pdo-basics\",\"name\":\"PDO Basics - Learn PHP with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\"},\"datePublished\":\"2026-05-20T15:32:43+00:00\",\"dateModified\":\"2026-05-21T14:42:00+00:00\",\"description\":\"Learn PDO basics in PHP including database connection, CRUD operations, prepared statements, and secure coding.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=pdo-basics#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=pdo-basics\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=pdo-basics#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/php\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced PHP > MySQL Database > PDO Basics\"}]},{\"@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":"PDO Basics - Learn PHP with GiGz.PK","description":"Learn PDO basics in PHP including database connection, CRUD operations, prepared statements, and secure coding.","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=pdo-basics","og_locale":"en_US","og_type":"article","og_title":"PDO Basics - Learn PHP with GiGz.PK","og_description":"Learn PDO basics in PHP including database connection, CRUD operations, prepared statements, and secure coding.","og_url":"https:\/\/gigz.pk\/php\/?lesson=pdo-basics","og_site_name":"Learn PHP with GiGz.PK","article_modified_time":"2026-05-21T14:42:00+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=pdo-basics","url":"https:\/\/gigz.pk\/php\/?lesson=pdo-basics","name":"PDO Basics - Learn PHP with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/php\/#website"},"datePublished":"2026-05-20T15:32:43+00:00","dateModified":"2026-05-21T14:42:00+00:00","description":"Learn PDO basics in PHP including database connection, CRUD operations, prepared statements, and secure coding.","breadcrumb":{"@id":"https:\/\/gigz.pk\/php\/?lesson=pdo-basics#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/php\/?lesson=pdo-basics"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/php\/?lesson=pdo-basics#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/php"},{"@type":"ListItem","position":2,"name":"Advanced PHP > MySQL Database > PDO Basics"}]},{"@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\/146","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=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}