Introduction
For beginners in website development, understanding how to use PHP scripts to connect to MySQL can be highly advantageous. This enables you to modify, view, or manage the tables within the MySQL database. In this article, we’ll guide you through the simplest methods to achieve this. Let’s get started!
Creating a MySQL Database (Optional)
This step is required if you do not have a MySQL database. If you are a Hostinger user, you can easily make a new one via Hostinger’s hPanel in a few steps:
- Locate MySQL Databases menu under the Databases section.
- Fill in the necessary fields and hit Create.
For instructions on how to create a MySQL database, check our cPanel tutorial. Keep in mind, however, that these are blank databases and you will need to fill in data before you can manipulate it.
Important! Write down the credentials of the MySQL database you just created for the next step. Don’t forget to remember the database username and username password as well!
Two Ways a PHP Script can Connect to MySQL
There are two methods to connect to a MySQL database using PHP: MySQLi and PDO.
MySQLi stands for MySQL Improved. It is a MySQL-exclusive extension that adds new features to a MySQL database’s interface. MySQLi is both procedural and object-oriented, with the former being the attribute inherited from the older version of MySQL.
PDO stands for PHP Data Object. 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.
Important! The original mysql_functions are deprecated and should not be used as they are unsafe and no longer being maintained or developed.
One of the most important features they both support is prepared statements, 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.
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.
Using MySQLi to Connect a PHP Script to MySQL
Follow these steps to use MySQLi to connect a PHP script to MySQL:
- Head over to File Manager -> public_html.
- Create a New File by clicking the icon from the sidebar menu.
- Save the file as databaseconnect.php. You can replace the name with whatever you like, just make sure it is using php as the extension.
- Double-click to open the file and copy-paste the provided code into it. Change the first four values with the credentials you noted earlier.
The provided code establishes a new connection to the MySQL server and checks if the connection is successful. If the connection is not successful, it will display a connection error message. If the connection is successful, it will display a success message. The connection will be automatically closed once the script ends.
Using PDO to Connect a PHP Script to MySQL
To use PDO to connect a PHP script to MySQL, follow these steps:
- In the public_html directory, create a file named pdoconfig.php and insert the provided code. Replace the placeholder values with your database information.
- Create another file named databaseconnect.php in the same directory, and insert the provided code. If you named the previous file differently, make sure to change the value of require_once.
The provided code creates a new PDO object with the database information and checks if the connection is successful. If the connection is not successful, it will display a connection error message. If the connection is successful, it will display a success message. The connection will be automatically closed once the script ends.
Checking Connectivity and Troubleshooting Common Errors
To check whether the connection is successful, access your domain like so: yourdomain/databaseconnect.php. If the connection is successful, you will see a success message. If the connection is not successful, you will see an error message indicating the issue.
Common errors include an incorrect password error and a cannot connect to MySQL server error. To troubleshoot these errors, you should check the database details and ensure they are correct. Additionally, checking the site error log can provide more information about the error.
Conclusion
In this tutorial, we have learned the very basic knowledge about how to connect a PHP script to a MySQL database using MySQLi and PDO. Connecting to a database is the first, most important step when working with more advanced scripts and configurations. If you face any issues following this guide, feel free to let us know in the comments below.
Author: Domantas G.
👉
Start your website with Hostinger – get fast, secure hosting here 👈
🔗 Read more from MinimaDesk:
- How to Disable xmlrpc.php in WordPress: A Comprehensive Guide
- The Ultimate Guide to WP-Content: Access, Upload, and Hide Your WordPress Directory
- 40 Essential WordPress SEO Tips to Boost Your Website’s Rankings
- How to Add and Customize RSS Feeds in WordPress
🎁 Download free premium WordPress tools from our Starter Tools page.