{"id":14528,"date":"2025-09-10T13:46:10","date_gmt":"2025-09-10T13:46:10","guid":{"rendered":"https:\/\/www.bodhost.com\/kb\/?p=14528"},"modified":"2026-02-26T14:02:06","modified_gmt":"2026-02-26T14:02:06","slug":"how-to-link-mysql-database-to-php-with-mysqli-and-pdo","status":"publish","type":"post","link":"https:\/\/www.bodhost.com\/kb\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\/","title":{"rendered":"How to Link MySQL Database to PHP With MySQLi and PDO"},"content":{"rendered":"<p>When creating dynamic webpages, connecting a PHP application to a MySQL database is a vital step. PDO (PHP Data Objects) and MySQLi (MySQL Improved Extension) are two well-liked ways that PHP provides for creating this connection.<\/p>\n<h2 data-start=\"49\" data-end=\"115\"><strong data-start=\"49\" data-end=\"113\">Method to Connect a MySQL Database to PHP Using MySQLi and PDO<\/strong><\/h2>\n<p>Let\u2019s explore the two methods:<\/p>\n<h4><strong>Method 1: Connect Using MySQLi<\/strong><\/h4>\n<p>MySQLi supports both object-oriented and procedural approaches.<\/p>\n<ol>\n<li><strong>Object-Oriented Example<\/strong>\n<pre>&lt;?php\r\n\r\n$servername = \u201clocalhost\u201d;\r\n\r\n$username\u00a0\u00a0 = \u201croot\u201d;\r\n\r\n$password\u00a0\u00a0 = \u201c\u201d;\r\n\r\n$database\u00a0\u00a0 = \u201ctest_db\u201d;\r\n\r\n\/\/ Create connection\r\n\r\n$conn = new mysqli($servername, $username, $password, $database);\r\n\r\n\/\/ Check connection\r\n\r\nif ($conn-&gt;connect_error) {\r\n\r\n\u00a0 \u00a0 \u00a0die(\u201cConnection failed: \u201d . $conn-&gt;connect_error);\r\n\r\n}\r\n\r\necho \u201cConnected successfully (MySQLi - OOP)\u201d;\r\n\r\n?&gt;<\/pre>\n<\/li>\n<\/ol>\n<ol start=\"2\">\n<li><strong>Procedural Example<\/strong>\n<pre>&lt;?php\r\n\r\n$servername = \u201clocalhost\u201d;\r\n\r\n$username\u00a0\u00a0 = \u201croot\u201d;\r\n\r\n$password\u00a0\u00a0 = \u201c\u201d;\r\n\r\n$database\u00a0\u00a0 = \u201ctest_db\u201d;\r\n\r\n\/\/ Create connection\r\n\r\n$conn = mysqli_connect($servername, $username, $password, $database);\r\n\r\n\/\/ Check connection\r\n\r\nif (!$conn) {\r\n\r\n\u00a0 \u00a0 die(\u201cConnection failed: \u201d . mysqli_connect_error());\r\n\r\n}\r\n\r\necho \u201cConnected successfully (MySQLi - Procedural)\u201d;\r\n\r\n?&gt;<\/pre>\n<\/li>\n<\/ol>\n<h4><strong>Method 2: Connect Using PDO<\/strong><\/h4>\n<p>PDO is more flexible as it supports multiple databases (MySQL, PostgreSQL, SQLite, etc.), making it a preferred option for larger projects.<\/p>\n<pre>&lt;?php\r\n$servername = \u201clocalhost\u201d;\r\n$username\u00a0\u00a0 = \u201croot\u201d;\r\n$password\u00a0\u00a0 = \u201c\u201d;\r\n$database\u00a0\u00a0 = \u201ctest_db\u201d;\r\n\r\ntry {\r\n$conn = new PDO(\u201cmysql:host=$servername;dbname=$database\u201d, $username, $password);\r\n\r\n\/\/ Set PDO error mode to exception\r\n$conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\r\necho \u201cConnected successfully (PDO)\u201d;\r\n} catch(PDOException $e) {\r\necho \u201cConnection failed: \u201d . $e-&gt;getMessage();\r\n}\r\n?&gt;<\/pre>\n<p>This way, you can connect a MySQL database to PHP using MySQLi and PDO. Hope you liked our article. For more informative articles, please visit our KB section regularly.<\/p>\n<p>Learn more tutorials about the <em><a href=\"https:\/\/www.bodhost.com\/kb\/php-script-to-test-mysql-database-connectivity\/\" target=\"_blank\" rel=\"noopener\">PHP Script to Test MySQL Database Connectivity<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When creating dynamic webpages, connecting a PHP application to a MySQL database is a vital step. PDO (PHP Data Objects) and MySQLi (MySQL Improved Extension) are two well-liked ways that&hellip;<\/p>\n<p><a href=\"https:\/\/www.bodhost.com\/kb\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\/\" class=\"more-link\">Read More<\/a><\/p>\n","protected":false},"author":21,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[1644,1646,1645],"class_list":["post-14528","post","type-post","status-publish","format-standard","hentry","category-sql","tag-link-mysql-database","tag-mysqli","tag-pdo"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Link MySQL Database to PHP With MySQLi and PDO<\/title>\n<meta name=\"description\" content=\"Learn how to connect a MySQL database to PHP using MySQLi (OOP &amp; procedural) and PDO with step-by-step examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.bodhost.com\/kb\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Link MySQL Database to PHP With MySQLi and PDO\" \/>\n<meta property=\"og:description\" content=\"Learn how to connect a MySQL database to PHP using MySQLi (OOP &amp; procedural) and PDO with step-by-step examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.bodhost.com\/kb\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\/\" \/>\n<meta property=\"og:site_name\" content=\"Knowledge Base - bodHOST\" \/>\n<meta property=\"article:published_time\" content=\"2025-09-10T13:46:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-02-26T14:02:06+00:00\" \/>\n<meta name=\"author\" content=\"Serena\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Serena\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\\\/\"},\"author\":{\"name\":\"Serena\",\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/#\\\/schema\\\/person\\\/ffd3f118493c99471ae5bd4790a55830\"},\"headline\":\"How to Link MySQL Database to PHP With MySQLi and PDO\",\"datePublished\":\"2025-09-10T13:46:10+00:00\",\"dateModified\":\"2026-02-26T14:02:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\\\/\"},\"wordCount\":146,\"publisher\":{\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/#organization\"},\"keywords\":[\"Link MySQL Database\",\"MySQLi\",\"PDO\"],\"articleSection\":[\"SQL\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\\\/\",\"url\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\\\/\",\"name\":\"How to Link MySQL Database to PHP With MySQLi and PDO\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/#website\"},\"datePublished\":\"2025-09-10T13:46:10+00:00\",\"dateModified\":\"2026-02-26T14:02:06+00:00\",\"description\":\"Learn how to connect a MySQL database to PHP using MySQLi (OOP & procedural) and PDO with step-by-step examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"SQL\",\"item\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/category\\\/sql\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Link MySQL Database to PHP With MySQLi and PDO\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/#website\",\"url\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/\",\"name\":\"Web Hosting Knowledge Base | bodHOST Hosting FAQ\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/#organization\"},\"alternateName\":\"Web Hosting Knowledge Base | bodHOST Hosting FAQ\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/#organization\",\"name\":\"Web Hosting Knowledge Base | bodHOST Hosting FAQ\",\"url\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/wp-content\\\/uploads\\\/2025\\\/10\\\/Profile-Pic.png\",\"contentUrl\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/wp-content\\\/uploads\\\/2025\\\/10\\\/Profile-Pic.png\",\"width\":240,\"height\":240,\"caption\":\"Web Hosting Knowledge Base | bodHOST Hosting FAQ\"},\"image\":{\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/#\\\/schema\\\/person\\\/ffd3f118493c99471ae5bd4790a55830\",\"name\":\"Serena\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8bf3c9f6220a13a4d46295ad7c38a0c9e351ca3a57d0e417580c8e8b83a12a50?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8bf3c9f6220a13a4d46295ad7c38a0c9e351ca3a57d0e417580c8e8b83a12a50?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8bf3c9f6220a13a4d46295ad7c38a0c9e351ca3a57d0e417580c8e8b83a12a50?s=96&d=mm&r=g\",\"caption\":\"Serena\"},\"url\":\"https:\\\/\\\/www.bodhost.com\\\/kb\\\/author\\\/seema\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Link MySQL Database to PHP With MySQLi and PDO","description":"Learn how to connect a MySQL database to PHP using MySQLi (OOP & procedural) and PDO with step-by-step examples.","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:\/\/www.bodhost.com\/kb\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\/","og_locale":"en_US","og_type":"article","og_title":"How to Link MySQL Database to PHP With MySQLi and PDO","og_description":"Learn how to connect a MySQL database to PHP using MySQLi (OOP & procedural) and PDO with step-by-step examples.","og_url":"https:\/\/www.bodhost.com\/kb\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\/","og_site_name":"Knowledge Base - bodHOST","article_published_time":"2025-09-10T13:46:10+00:00","article_modified_time":"2026-02-26T14:02:06+00:00","author":"Serena","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Serena","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.bodhost.com\/kb\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\/#article","isPartOf":{"@id":"https:\/\/www.bodhost.com\/kb\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\/"},"author":{"name":"Serena","@id":"https:\/\/www.bodhost.com\/kb\/#\/schema\/person\/ffd3f118493c99471ae5bd4790a55830"},"headline":"How to Link MySQL Database to PHP With MySQLi and PDO","datePublished":"2025-09-10T13:46:10+00:00","dateModified":"2026-02-26T14:02:06+00:00","mainEntityOfPage":{"@id":"https:\/\/www.bodhost.com\/kb\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\/"},"wordCount":146,"publisher":{"@id":"https:\/\/www.bodhost.com\/kb\/#organization"},"keywords":["Link MySQL Database","MySQLi","PDO"],"articleSection":["SQL"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.bodhost.com\/kb\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\/","url":"https:\/\/www.bodhost.com\/kb\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\/","name":"How to Link MySQL Database to PHP With MySQLi and PDO","isPartOf":{"@id":"https:\/\/www.bodhost.com\/kb\/#website"},"datePublished":"2025-09-10T13:46:10+00:00","dateModified":"2026-02-26T14:02:06+00:00","description":"Learn how to connect a MySQL database to PHP using MySQLi (OOP & procedural) and PDO with step-by-step examples.","breadcrumb":{"@id":"https:\/\/www.bodhost.com\/kb\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.bodhost.com\/kb\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.bodhost.com\/kb\/how-to-link-mysql-database-to-php-with-mysqli-and-pdo\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"SQL","item":"https:\/\/www.bodhost.com\/kb\/category\/sql\/"},{"@type":"ListItem","position":2,"name":"How to Link MySQL Database to PHP With MySQLi and PDO"}]},{"@type":"WebSite","@id":"https:\/\/www.bodhost.com\/kb\/#website","url":"https:\/\/www.bodhost.com\/kb\/","name":"Web Hosting Knowledge Base | bodHOST Hosting FAQ","description":"","publisher":{"@id":"https:\/\/www.bodhost.com\/kb\/#organization"},"alternateName":"Web Hosting Knowledge Base | bodHOST Hosting FAQ","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.bodhost.com\/kb\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.bodhost.com\/kb\/#organization","name":"Web Hosting Knowledge Base | bodHOST Hosting FAQ","url":"https:\/\/www.bodhost.com\/kb\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.bodhost.com\/kb\/#\/schema\/logo\/image\/","url":"https:\/\/www.bodhost.com\/kb\/wp-content\/uploads\/2025\/10\/Profile-Pic.png","contentUrl":"https:\/\/www.bodhost.com\/kb\/wp-content\/uploads\/2025\/10\/Profile-Pic.png","width":240,"height":240,"caption":"Web Hosting Knowledge Base | bodHOST Hosting FAQ"},"image":{"@id":"https:\/\/www.bodhost.com\/kb\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.bodhost.com\/kb\/#\/schema\/person\/ffd3f118493c99471ae5bd4790a55830","name":"Serena","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/8bf3c9f6220a13a4d46295ad7c38a0c9e351ca3a57d0e417580c8e8b83a12a50?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/8bf3c9f6220a13a4d46295ad7c38a0c9e351ca3a57d0e417580c8e8b83a12a50?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8bf3c9f6220a13a4d46295ad7c38a0c9e351ca3a57d0e417580c8e8b83a12a50?s=96&d=mm&r=g","caption":"Serena"},"url":"https:\/\/www.bodhost.com\/kb\/author\/seema\/"}]}},"_links":{"self":[{"href":"https:\/\/www.bodhost.com\/kb\/wp-json\/wp\/v2\/posts\/14528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bodhost.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bodhost.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bodhost.com\/kb\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bodhost.com\/kb\/wp-json\/wp\/v2\/comments?post=14528"}],"version-history":[{"count":6,"href":"https:\/\/www.bodhost.com\/kb\/wp-json\/wp\/v2\/posts\/14528\/revisions"}],"predecessor-version":[{"id":14649,"href":"https:\/\/www.bodhost.com\/kb\/wp-json\/wp\/v2\/posts\/14528\/revisions\/14649"}],"wp:attachment":[{"href":"https:\/\/www.bodhost.com\/kb\/wp-json\/wp\/v2\/media?parent=14528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bodhost.com\/kb\/wp-json\/wp\/v2\/categories?post=14528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bodhost.com\/kb\/wp-json\/wp\/v2\/tags?post=14528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}