Welcome to the new series of articles where we will learn backend web programming with core PHP. We already have some articles available on our site regarding front-end development with HTML and CSS. This series of articles will mainly focus on back-end development, including registration system, PHP CRUD operations, image or file upload, server-side validation, and much more. So let's start the first article and learn how to connect MySQL database with PHP.
Install Xamp server:
- First of all, download and install the Xamp server to make things straightforward. Visit apachefriends website to download the latest version of the Xamp server.
- After downloading install the Xamp server on your system.
- Run Xamp control panel and start apache and MySQL services.
Now your Xamp server is running and you can create PHP projects. Lets first create the MySQL database.
Creating MySQL database:
- Enter "localhost/phpmyadmin/" in any browser. You will see the following interface.
- On the left sidebar, click on "new" to create a new database.
- Now enter the database name and click on the "create" button.
Creating first PHP project:
- Open the folder where your Xamp server is installed and search for a folder "htdocs".
- Inside the "htdocs" folder, create a new folder and give it any name.i.e (library).
- Open this folder in any text editor like sublime text, visual studio code, etc.
PHP MySQL Connect to database:
- The best coding practice for database connectivity is to create a file "db.inc.php".
- Include All your pure PHP files inside an includes folder to separate them from other files.
- Inside the "db.inc.php" file, write your database connectivity code.
- Use mysqli_connect() PHP function which takes four parameters. server name, username, password, and database name.
<?php $server_name ="localhost"; $db_username = "root"; $db_password =""; $db_name ="library"; $conn = mysqli_connect($server_name, $db_username, $db_password, $db_name); if(!$conn){ die("failed to connect to the server". mysqli_connect_error()); }else{ echo "connected successfully"; } ?>
The mysqli_connect() function is used to establish a connection to the database. It takes four parameters as illustrated in the above code. In case of the Xamp server(local server), the server name is always "localhost". The default username is root and the password is none. If it opens connection successfully it returns an object to represent the connection. On the other hand, it returns false if it fails.
The die() function is an inbuilt PHP function used to exit the current PHP script and print a message. Its functionality is exactly the same as exit() function in PHP.
The mysqli_connect_error() function is also an inbuilt PHP function. It returns null when the connection is successful. In case of failure, it returns a string containing the description of the error.
Using db.inc.php file:
The "db.inc.php" file can be used by including it to the file where database connectivity is required. There are two ways to include it. We can use both the require and include statements.
<?php require "db.inc.php"; ?> //OR use <?php include "db.inc.php"; ?>
The require and include statements are almost identical. The only difference is that the include statement generates a warning if the file is not found and continues the execution of the rest of the script. But require statement generates a fatal error and stops the script.
Now let's end this article here. In the next one, we will learn to register users on our system. Hope you enjoyed this article. Kindly share it with your friends and support us.
0 Comments