{"id":746,"date":"2019-11-23T07:55:19","date_gmt":"2019-11-23T07:55:19","guid":{"rendered":"http:\/\/blog.hostinger.io\/hostinger-tutorials\/uncategorized\/how-to-connect-php-to-mysql\/"},"modified":"2026-10-15T05:34:30","modified_gmt":"2026-10-15T05:34:30","slug":"how-to-connect-php-to-mysql","status":"publish","type":"post","link":"\/my\/tutorials\/how-to-connect-php-to-mysql\/","title":{"rendered":"How to connect PHP to a MySQL database using PDO or MySQLi"},"content":{"rendered":"<p class=\"wp-block-paragraph\">To connect PHP to a MySQL database, use MySQLi or PDO with your database hostname, username, password, and database name. MySQLi is designed for MySQL, while PDO provides a consistent interface for connecting to different database systems.<\/p><p class=\"wp-block-paragraph\">Both methods support prepared statements for executing SQL queries securely.<\/p><p class=\"wp-block-paragraph\">In this tutorial, you&rsquo;ll learn how to choose between MySQLi and PDO, connect PHP to MySQL using either method, and test your database connection.<\/p><p>\n\n\n\n<\/p><h2 id=\"h-creating-a-mysql-database-optional\" class=\"wp-block-heading h-t-title-2\">Creating a MySQL Database (Optional)<\/h2><p class=\"wp-block-paragraph\">This step is required if you do not have a <strong>MySQL database<\/strong>. If you are a Hostinger user, you can easily make a new one via Hostinger dashboard in a few steps:<\/p><ol class=\"wp-block-list\">\n<li>Locate <strong>MySQL Databases <\/strong>menu under the&nbsp;<strong>Databases&nbsp;<\/strong>section.<\/li>\n\n\n\n<li>Fill in the necessary fields and hit&nbsp;<strong>Create<\/strong>.<\/li>\n<\/ol><div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a data-click-id=\"hgr-post-content-link-hostinger-com-my-tutorials-wp-content-uploads-sites-2-2022-1\" href=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2022\/10\/The-MySQL-Databases-section-in-the-hPanel.png\"><img decoding=\"async\" width=\"1132\" height=\"421\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/10\/The-MySQL-Databases-section-in-the-hPanel.png\/w=1024,h=1024,fit=scale-down\" alt=\"The MySQL Databases section in the hPanel, showing you how to create a new MySQL database\" class=\"wp-image-66436\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-MySQL-Databases-section-in-the-hPanel.png 1132w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-MySQL-Databases-section-in-the-hPanel-300x112.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-MySQL-Databases-section-in-the-hPanel-1024x381.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-MySQL-Databases-section-in-the-hPanel-150x56.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2022\/10\/The-MySQL-Databases-section-in-the-hPanel-768x286.png 768w\" sizes=\"(max-width: 1132px) 100vw, 1132px\" \/><\/a><\/figure><\/div><p class=\"wp-block-paragraph\"><a data-click-id=\"hgr-post-content-link-hostinger-com-my-tutorials-wp-content-uploads-sites-2-2019-1\" aria-label=\"Mysqul database hostinger\" href=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/11\/mysqul-database-hostinger.png\"><\/a><\/p><p><div class=\"announcement-block announcement-block--important\">\n            <span class=\"announcement-block__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path fill-rule=\"evenodd\" clip-rule=\"evenodd\"\n                          d=\"M12 22.5C17.799 22.5 22.5 17.799 22.5 12C22.5 6.20101 17.799 1.5 12 1.5C6.20101 1.5 1.5 6.20101 1.5 12C1.5 17.799 6.20101 22.5 12 22.5ZM13.637 7.65198C13.637 6.74791 12.9041 6.01501 12 6.01501C11.0959 6.01501 10.363 6.74791 10.363 7.65198C10.5335 9.53749 10.875 13.383 10.875 13.383C10.875 14.0043 11.3787 14.508 12 14.508C12.6213 14.508 13.125 14.0043 13.125 13.383V13.38L13.637 7.65198ZM11.9927 15.714C11.3714 15.714 10.8677 16.2177 10.8677 16.839C10.8677 17.4603 11.3714 17.964 11.9927 17.964H12.0073C12.6286 17.964 13.1323 17.4603 13.1323 16.839C13.1323 16.2177 12.6286 15.714 12.0073 15.714H11.9927Z\"\n                          fill=\"#FEA419\"\/>\n                <\/svg>\n                Important\n            <\/span>\n            <p class=\"announcement-block__content\">\n                <strong>Important!<\/strong> Write down the credentials of the MySQL database you just created for the next step. Don&rsquo;t forget to remember the database username and username password as well! \n            <\/p><\/div>\n\n\n\n<\/p><h2 id=\"h-two-ways-a-php-script-can-connect-to-mysql\" class=\"wp-block-heading h-t-title-2\">PDO vs. MySQLi: Which should you use?<\/h2><p class=\"wp-block-paragraph\">There are two methods to connect to a MySQL database using PHP: <strong>MySQLi<\/strong>&nbsp;and <strong>PDO<\/strong>.<\/p><p class=\"wp-block-paragraph\"><strong>MySQLi <\/strong>stands for<strong> MySQL Improved.&nbsp;<\/strong>It is a MySQL-exclusive extension that adds new features to a MySQL database&rsquo;s interface. MySQLi is both procedural and object-oriented, with the former being the attribute inherited from the older version of MySQL.<\/p><p class=\"wp-block-paragraph\">The original MySQL breaks down a task into linear, step-by-step procedures, which makes it difficult to modify because you have to edit the code from the top. Meanwhile, MySQLi sees data as a set of interchangeable objects with functions, allowing users to add or remove data easily.<\/p><p class=\"wp-block-paragraph\"><strong>PDO <\/strong>stands for <strong>PHP Data Object<\/strong><strong>. <\/strong>Unlike MySQLi, PDO is only object-oriented and supports a number of different database types that use PHP, such as MySQL, MSSQL, Informix, and PostgreSQL.<\/p><p><div class=\"announcement-block announcement-block--important\">\n            <span class=\"announcement-block__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path fill-rule=\"evenodd\" clip-rule=\"evenodd\"\n                          d=\"M12 22.5C17.799 22.5 22.5 17.799 22.5 12C22.5 6.20101 17.799 1.5 12 1.5C6.20101 1.5 1.5 6.20101 1.5 12C1.5 17.799 6.20101 22.5 12 22.5ZM13.637 7.65198C13.637 6.74791 12.9041 6.01501 12 6.01501C11.0959 6.01501 10.363 6.74791 10.363 7.65198C10.5335 9.53749 10.875 13.383 10.875 13.383C10.875 14.0043 11.3787 14.508 12 14.508C12.6213 14.508 13.125 14.0043 13.125 13.383V13.38L13.637 7.65198ZM11.9927 15.714C11.3714 15.714 10.8677 16.2177 10.8677 16.839C10.8677 17.4603 11.3714 17.964 11.9927 17.964H12.0073C12.6286 17.964 13.1323 17.4603 13.1323 16.839C13.1323 16.2177 12.6286 15.714 12.0073 15.714H11.9927Z\"\n                          fill=\"#FEA419\"\/>\n                <\/svg>\n                Important\n            <\/span>\n            <p class=\"announcement-block__content\">\n                <strong>Important!<\/strong> The original <strong>mysql_ <\/strong>functions are <strong>deprecated<\/strong> and should not be used as they are unsafe and no longer being maintained or developed. \n            <\/p><\/div>\n\n\n\n<\/p><p class=\"wp-block-paragraph\">One of the most important features they both support is <strong>prepared statements<\/strong>, which accelerates the time needed for MySQL to execute the same query multiple times. It is also used to prevent SQL injection attacks when making changes to the database or inserting user-supplied input into a database query or statement.<\/p><p class=\"wp-block-paragraph\">Whichever method you use, you will need the correct information so you can connect to the MySQL database you have made. This is where the MySQL database details you have previously saved come in handy.<\/p><p class=\"wp-block-paragraph\">You also need the correct <strong>server name<\/strong> or <strong>hostname <\/strong>for the configuration. Hostinger uses <strong>&ldquo;localhost&rdquo; <\/strong>as its MySQL server&rsquo;s hostname. In general, this is the name that you&rsquo;ll want to use if you uploaded your PHP script to the same server as the database.<\/p><p class=\"wp-block-paragraph\">In contrast, if you&rsquo;re using a <a data-click-id=\"hgr-post-content-link-mysql-remote-connection\" href=\"\/my\/tutorials\/mysql\/how-to-grant-remote-access-mysql-vps\/\">MySQL remote connection<\/a> to connect to a database, you will have to use the <strong>IP address<\/strong> of the MySQL server. For more details, contact your hosting provider so they could provide you with the correct information on what to use as the hostname.<\/p><p class=\"wp-block-paragraph\"><strong>Which should you choose?<\/strong> PDO is a practical default for new PHP projects, especially if you want a consistent way to work with different database systems. MySQLi is equally suitable when your application specifically uses MySQL or already relies on MySQLi.<\/p><p class=\"wp-block-paragraph\">Both support prepared statements, so neither is inherently less secure. Security depends on how you write and execute your database queries.<\/p><p class=\"wp-block-paragraph\">The examples below demonstrate both methods so you can choose the one that fits your project.<\/p><h2 class=\"wp-block-heading h-t-title-2\" id=\"h-how-to-connect-php-to-mysql-using-mysqli\">How to connect PHP to MySQL using MySQLi<\/h2><p class=\"wp-block-paragraph\">Follow these steps to use MySQLi to connect a PHP script to MySQL:<\/p><ol class=\"wp-block-list\">\n<li>Head over to <strong>File Manager<\/strong> -&gt; <strong>public_html.<\/strong><\/li>\n<\/ol><div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a data-click-id=\"hgr-post-content-link-hostinger-com-my-tutorials-wp-content-uploads-sites-2-2023-0\" href=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/03\/filemanager-publichtml-highlighted.png\"><img decoding=\"async\" width=\"1157\" height=\"240\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/03\/filemanager-publichtml-highlighted.png\/w=1024,h=1024,fit=scale-down\" alt=\"The public_html folder location in the File Manager\" class=\"wp-image-82396\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/03\/filemanager-publichtml-highlighted.png 1157w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/03\/filemanager-publichtml-highlighted-300x62.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/03\/filemanager-publichtml-highlighted-1024x212.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/03\/filemanager-publichtml-highlighted-150x31.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/03\/filemanager-publichtml-highlighted-768x159.png 768w\" sizes=\"(max-width: 1157px) 100vw, 1157px\" \/><\/a><\/figure><\/div><ol start=\"2\" class=\"wp-block-list\">\n<li>Create a <strong>New File <\/strong>by clicking the icon from the sidebar menu.<\/li>\n<\/ol><div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a data-click-id=\"hgr-post-content-link-hostinger-com-my-tutorials-wp-content-uploads-sites-2-2023-0-2\" href=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/03\/filemanager-newfile-highlighted.png\"><img decoding=\"async\" width=\"1024\" height=\"336\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/03\/filemanager-newfile-highlighted.png\/w=1024,h=1024,fit=scale-down\" alt=\"The menu bar in the File Manager, showing how to create a New File inside public_html\" class=\"wp-image-82398\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/03\/filemanager-newfile-highlighted.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/03\/filemanager-newfile-highlighted-300x98.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/03\/filemanager-newfile-highlighted-150x49.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/03\/filemanager-newfile-highlighted-768x252.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><ol start=\"3\" class=\"wp-block-list\">\n<li>Save the file as <strong>databaseconnect.php<\/strong>. You can replace the name with whatever you like, just make sure it is using php as the extension.<\/li>\n<\/ol><div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a data-click-id=\"hgr-post-content-link-hostinger-com-my-tutorials-wp-content-uploads-sites-2-2023-0-3\" href=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/03\/filemanager-newfile-databaseconnectphp-create-highlighted.png\"><img decoding=\"async\" width=\"1024\" height=\"565\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/03\/filemanager-newfile-databaseconnectphp-create-highlighted.png\/w=1024,h=1024,fit=scale-down\" alt=\"The New File popup appears in the File Manager for users to create a new PHP file inside public_html\" class=\"wp-image-82399\" srcset=\"https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/03\/filemanager-newfile-databaseconnectphp-create-highlighted.png 1024w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/03\/filemanager-newfile-databaseconnectphp-create-highlighted-300x165.png 300w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/03\/filemanager-newfile-databaseconnectphp-create-highlighted-150x83.png 150w, https:\/\/www.hostinger.com\/my\/tutorials\/wp-content\/uploads\/sites\/45\/2023\/03\/filemanager-newfile-databaseconnectphp-create-highlighted-768x423.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><ol start=\"4\" class=\"wp-block-list\">\n<li>Double-click to open the file and copy-paste the following lines of code into it. Change the first four values below <strong>&lt;?php<\/strong> with the credentials you noted earlier:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$servername = \"localhost\";\n$database = \"your_database\";\n$username = \"your_username\";\n$password = \"your_password\";\n\nmysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);\n\ntry {\n    $conn = mysqli_connect($servername, $username, $password, $database);\n    mysqli_set_charset($conn, \"utf8mb4\");\n\n    echo \"Connected successfully\";\n    mysqli_close($conn);\n} catch (mysqli_sql_exception $e) {\n    error_log($e-&gt;getMessage());\n    http_response_code(500);\n    echo \"Database connection failed.\";\n}<\/pre><h3 id=\"h-mysqli-code-explained\" class=\"wp-block-heading h-t-title-3\">How the MySQLi connection works<\/h3><p class=\"wp-block-paragraph\">This example uses MySQLi&rsquo;s procedural interface. The <code>mysqli_connect()<\/code> function establishes a connection using your database hostname, username, password, and database name.<\/p><p class=\"wp-block-paragraph\">The <code>mysqli_set_charset()<\/code> function configures UTF-8 encoding, while the <code>try...catch<\/code> block handles connection errors. If the connection succeeds, the script displays a confirmation message. Otherwise, it logs the error and displays a general failure message.<\/p><p class=\"wp-block-paragraph\">MySQLi also supports an object-oriented approach using <code>new mysqli()<\/code>. Both styles can establish database connections and execute queries.<\/p><p class=\"wp-block-paragraph\">For queries that include user-provided values, use prepared statements with <code>mysqli_prepare()<\/code> to help prevent SQL injection.<\/p><h2 id=\"h-using-pdo-to-connect-a-php-script-to-mysql\" class=\"wp-block-heading h-t-title-2\">How to connect PHP to MySQL using PDO<\/h2><p class=\"wp-block-paragraph\">The other method using PHP script to connect to MySQL is by using PDO. This is similar to the previous method, but with a slight variation:<\/p><ol class=\"wp-block-list\">\n<li>In the<strong> public_html<\/strong>, create a file named<strong> pdoconfig.php<\/strong> and insert the following code. As always, don&rsquo;t forget to replace the placeholder values with your database information. <strong>Save and Close<\/strong> it once you&rsquo;re done:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$host = \"localhost\";\n$dbname = \"u123456789_DatabaseName\";\n$username = \"u123456789_User\";\n$password = \"MyStr0ngPa$!\";<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Create another file named <strong>databaseconnect.php<\/strong> in the same directory, but with the following code. If you named the previous file differently, make sure to change the value of <strong>require_once<\/strong>.<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nrequire_once 'pdoconfig.php';\n\ntry {\n    $conn = new PDO(\n        \"mysql:host=$host;dbname=$dbname;charset=utf8mb4\",\n        $username,\n        $password,\n        [PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION]\n    );\n\n    echo \"Connected successfully\";\n} catch (PDOException $e) {\n    error_log($e-&gt;getMessage());\n    http_response_code(500);\n    echo \"Database connection failed.\";\n}<\/pre><h3 id=\"h-pdo-code-explained\" class=\"wp-block-heading h-t-title-3\"><strong>How the PDO connection works<\/strong><\/h3><p class=\"wp-block-paragraph\">A PDO database connection requires you to create a new PDO object with a <strong>Data Source Name (DSN), Username,<\/strong> and <strong>Password<\/strong>.<\/p><p class=\"wp-block-paragraph\">The DSN defines the type of database, the name of the database, and any other information related to the database if required. These are the variables and values we stated inside the <strong>pdoconfig.php&nbsp;<\/strong>file, referenced one time by the line <strong>require_once&nbsp;<\/strong>in the&nbsp;<strong>databaseconnect.php.<\/strong><\/p><p class=\"wp-block-paragraph\">In the latter, you will find the <strong>try&hellip;catch.. <\/strong>code. This means that the script will <strong>try<\/strong> to MySQL connect using the code provided, but if there is a problem, the code in the <strong>catch<\/strong> section will run. You can use the <strong>catch block<\/strong> to display connect error messages or run alternate code if the <strong>try block<\/strong> fails.<\/p><p class=\"wp-block-paragraph\">If the connection is successful, it will print out the message &ldquo;<strong>Connected to $dbname at $host successfully.<\/strong>&rdquo; However, if the attempt fails, the catch code will show a simple error message and kill the script.<\/p><p class=\"wp-block-paragraph\">Unlike when using MySQLi, after you&rsquo;re finished using a PDO script, you don&rsquo;t need to manually close the connection. It is automatically closed when the PDO object is destroyed, or your script ends.<\/p><h2 id=\"h-checking-connectivity-and-troubleshooting-common-errors\" class=\"wp-block-heading h-t-title-2\">How to test your PHP MySQL connection<\/h2><p class=\"wp-block-paragraph\">To check whether the connection is successful, access your domain like so: <strong>yourdomain\/databaseconnect.php<\/strong>. If you name the PHP file with something different, make sure to change it accordingly.<\/p><p class=\"wp-block-paragraph\">You will see &ldquo;<strong>Connected<\/strong> <strong>successfully<\/strong>&rdquo; or variants of this message if everything is running without any issue.<\/p><p class=\"wp-block-paragraph\">If the connection fails, verify your database credentials, hostname, and database name. Also make sure the required MySQLi or PDO MySQL extension is enabled in your PHP environment.<\/p><p class=\"wp-block-paragraph\">For additional details, check your PHP error logs through your hosting control panel or server configuration. The location of these logs depends on your hosting environment and PHP settings.<\/p><p class=\"wp-block-paragraph\">Once you&rsquo;ve confirmed that the connection works, remove the standalone test script or restrict access to it. Avoid displaying detailed database errors or exposing credentials on a live website.<\/p><h3 id=\"h-incorrect-password-error\" class=\"wp-block-heading h-t-title-3\">Incorrect Password Error<\/h3><p class=\"wp-block-paragraph\">This error happens if we change the password or any credential in the PHP code (but do not change it in the actual database).<\/p><p class=\"wp-block-paragraph\">In case you see an &ldquo;<strong>Access denied<\/strong>&rdquo; or &ldquo;<strong>Could not connect to database<\/strong>&rdquo; message accompanied by &ldquo;<strong>(using password: YES)<\/strong>&rdquo; at the end, the first thing to do is to check the database details. There could be a typo or a part that&rsquo;s missing.<\/p><h3 id=\"h-cannot-connect-to-mysql-server\" class=\"wp-block-heading h-t-title-3\">Cannot Connect to MySQL Server<\/h3><p class=\"wp-block-paragraph\">If you get &ldquo;<strong>Can&rsquo;t connect to MySQL server on &lsquo;server&rsquo; (110)<\/strong>&rdquo; in MySQLi, it means the script did not get a response from a server. This happens when we set &ldquo;<strong>server&rdquo;<\/strong> instead of &ldquo;<strong>localhost&rdquo;<\/strong>&nbsp;as the<strong> $servername,<\/strong> and this name is not recognized.<\/p><p class=\"wp-block-paragraph\">The error message in PDO will look like &ldquo;<strong>Connection failed: SQLSTATE[Hy000] [2002]&rdquo; <\/strong>followed by more details stating that the My SQL host was not found. But the way to troubleshoot it is the same as the above.<\/p><p class=\"wp-block-paragraph\">And of course, it is always important to remember one golden rule of troubleshooting an error: <a data-click-id=\"hgr-post-content-link-checking-your-site-error-log\" href=\"\/my\/tutorials\/website\/how-to-check-your-website-error-log\/\">checking your site error log<\/a>.<\/p><p class=\"wp-block-paragraph\">The log can be found in the same folder where the script is running. For example, if we are running a script in <strong>public_html<\/strong>, we will find the <strong>error_log<\/strong> in the same folder.<\/p><h2 id=\"h-conclusion\" class=\"wp-block-heading h-t-title-2\">Next steps: Run your first MySQL query<\/h2><p class=\"wp-block-paragraph\">Once your PHP script successfully connects to MySQL, the next step is to retrieve or update data in your database.<\/p><p class=\"wp-block-paragraph\">Start with a simple <code>SELECT<\/code> query to retrieve records from an existing table. When working with user input, use prepared statements through MySQLi or PDO instead of inserting values directly into SQL queries. This helps protect your application against SQL injection.<\/p><p class=\"wp-block-paragraph\">Before deploying your application, make sure your database credentials are stored securely, remove unnecessary connection-test scripts, and review your database user&rsquo;s permissions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To connect PHP to a MySQL database, use MySQLi or PDO with your database hostname, username, password, and database name. MySQLi is designed for MySQL, while PDO provides a consistent interface for connecting to different database systems. Both methods support prepared statements for executing SQL queries securely. In this tutorial, you&rsquo;ll learn how to choose [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/my\/tutorials\/how-to-connect-php-to-mysql\/\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":2,"featured_media":137016,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to Connect PHP to MySQL Database with PDO and MySQLi","rank_math_description":"Our complete guide will show you how to connect PHP to MySQL database using two different methods: MySQLi and PDO. Complete scripts provided.","rank_math_focus_keyword":"php mysql connect","footnotes":""},"categories":[22663],"tags":[],"class_list":["post-746","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-connect-php-to-mysql\/","default":0},{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/como-conectar-php-com-mysql\/","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/connecter-php-base-donnees-mysql\/","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/conectar-php-mysql\/","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/koneksi-database-php\/","default":0},{"locale":"it-IT","link":"https:\/\/www.hostinger.com\/it\/tutorial\/collegare-php-a-mysql\/","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-connect-php-to-mysql\/","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-connect-php-to-mysql\/","default":0},{"locale":"en-GB","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-connect-php-to-mysql\/","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-connect-php-to-mysql\/","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-connect-php-to-mysql\/","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-connect-php-to-mysql\/","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-connect-php-to-mysql\/","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/conectar-php-mysql\/","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/conectar-php-mysql\/","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/conectar-php-mysql\/","default":0},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/como-conectar-php-com-mysql\/","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/comments?post=746"}],"version-history":[{"count":37,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/746\/revisions"}],"predecessor-version":[{"id":137015,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/posts\/746\/revisions\/137015"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/media\/137016"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/media?parent=746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/categories?post=746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-json\/wp\/v2\/tags?post=746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}