Connecting MySQLi with PHP

Creating Database Connections with MySQLi and PHP

Linking PHP and databases is a foundation in web programming. By connecting the database with PHP, we can interact with the database further such as creating, editing, deleting and displaying the contents of the database.

Reasons to use MySQLi

MySQLi is a new function of MySQL, namely MySQL Improved. MySQLi is said to be more secure and faster than MySQL. MySQLi can be run with PHP version 5 and above, if it is still below 5 then update PHP first.

One important function that MySQLi also supports is prepared statements. This function will speed up the time required by MySQL to run the same query many times. Prepared statements also serve to prevent SQL injection attacks from executing changes in the database.

MySQL functions are deprecated because they are not safe and are no longer maintained or developed.

Connecting MySQLi database with PHP

  • Create a new database with the desired name, for example: student.
  • Create a new folder in htdocs with the name connection and containing the file connection.php
  • Enter the following syntax in PHP

$db=mysqli_connect(“servername”,”username”,”password”,”database”);

if (mysqli_connect_errno()){

     echo “Database connection failed : ” . mysqli_connect_error();

}

Line 1: The first parameter enter the host / server, the second parameter is the username, the third parameter is the password (if any), and the fourth parameter is the database used.

Lines 2-4 : Here we check whether the database error or not, if an error will appear in the “Database connection failed: error type”. If an error occurs it will appear on the line where the error is and be resolved in the existing way.

If there are no errors and the database has been created, the database is connected and ready to be used in PHP.

Conclusion and Closing

If the database is connected to PHP, then we can use the database according to our wishes in a website and it really works on a website.

The need for digital IT is needed in daily activities, Bead IT Consultant is the right choice as your partner, visit our website by clicking this link: www.beadgroup.com .