All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Database Connection In PHP With MySQL

Last Updated : Jul 1, 2023

Database Connection In PHP With MySQL

In this article we will show you the solution of database connection in PHP with MySQL, as a database system for PHP, it is the most often used. Query Structured Language (SQL).

A server-based database system called MySQL is used. Large and small applications can both benefit from MySQL.

The database system MySQL is incredibly quick, dependable, and user-friendly. It employs typical SQL.

Using a variety of operating systems, MySQL can be built. Now move to the concept of database connection in php with mysql.

Step By Step Guide On Database Connection In PHP With MySQL :-

To establish a database connection, PHP offers the mysql connect() function.

Using a connection given by that of the mysql connect() method, this function just requires one parameter.

Whenever you choose, you can exit as from MySQL database by using the mysql close function in PHP().

Moreover, MySQLi provides a procedural method for connecting a PHP script to a MySQL database.

<?php
$servername = "localhost";
$username = "user";
$password = "password";
$conn = new mysqli($servername,
  $username, $password);
if ($conn->connect_error) {
die("Connection failed: "
 . $conn->connect_error);
}
echo "Connected successfully";
?>
  1. The first line of a PHP script is <?php. On the server, the PHP script is run, and the result is transmitted to the web browser as plain HTML.
  2. $servername is the name of the host on which your server is located; this host is often localhost. You can give a host name or IP address of the MySQL server using the host argument.
  3. The username, i.e. user, will be $username. The username of the MySQL server is specified via the username argument.
  4. $password will represent the password you used it to access PHPMyAdmin, and it will be that. You can set the MySQL server password using the password argument.
  5. The primary PHP function mysqli connect is next (). A connection is established with the chosen database. the object-oriented syntax of the mysqli() function in PHP, which opens a database connection.
  6. The connection variable $conn is utilised to further determine whether the connection has been established or not. This variable can be used to interact with the database if such connection has been created.
  7. An "if statement" follows. That is the section of the code that indicates whether or not the connection was made.
  8. The notice "Connection failed" is displayed when the connection breaks down. The message is printed by the die function, which then leaves the script.
  9. It says, "Connected successfully," if the connection was successful.
  10. We finish by closing our code.

Conclusion :-

Hence, we were able to understand the principle of database connection with PHP using MySQL.

A database connection can be made using PHP's mysql connect() function, as we also taught.

This function only needs one parameter and uses the connection provided by the mysql connect() method.

By utilising the PHP mysql shut function, you can exit from the MySQL database whenever you want().

Moreover, MySQLi offers a procedural way to link a PHP script to the MySQL database.

I hope this article on database connection in PHP with MySQL helps you and the steps and method mentioned above are easy to follow and implement.