All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Database Connectivity In PHP With Example

Last Updated : Mar 11, 2024

Database Connectivity In PHP With Example

In this article we will show you the solution of database connectivity in PHP with example, the mysqli constructor mysqli_connect() function is available in PHP to open a database connection.

This function accepts six arguments and, in the event of success, returns a MySQL link identifier; otherwise, it returns FALSE.

A connection between a MySQL database and a PHP script must be established before you can access or add anything to the database.

This article explains how to connect with MySQL using the PHP Data Objects and MySQLi Extension.

The conventional, legacy mysql_ functions are deprecated, thus we won't discuss them in this manual.

Only MySQL databases are supported by the extension known as MySQLi.

It provides both an object-oriented and procedural interface, enabling access to new functionalities present in MySQL systems (version 4.1 and above).

It does not support client-side prepared statements, only server-side prepared statements.

We will now discuss the idea of database connectivity in php with example.

Step By Step Guide On Database Connectivity In PHP With Example :-

Code 1

$servername = "localhost";
$database = "database";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
echo “Connected successfully”;

Code 2

      <title>TalkersCode MySQL Server</title>
         $dbhost = 'localhost';
         $dbuser = 'root';
         $dbpass = 'root@123';
         $mysqli = new mysqli($dbhost, $dbuser, $dbpass);
         if($mysqli->connect_errno ) {
            printf("Connect failed: %s<br />", $mysqli->connect_error);
         printf('Connected successfully.<br />');
  1. Declare the database credentials: $servername, $database, $username, and $password.
  2. Establish a connection to the database server using mysqli_connect() function, passing the servername, username, password, and database as parameters.
  3. Check if the connection is successful using the connect_error property of the $conn object. If the connection failed, display an error message using the die() function.
  4. If the connection is successful, display a success message using the echo() function.
  5. Utilize the mysqli_close() method to terminate the database connection.

Conclusion :-

As a result, we have successfully learned database connectivity in php with example. This post described two ways to connect with an MySQL database using PHP.

There are benefits to both MySQLi and PDO. You should keep in mind, nonetheless, that MySQLi is only employed with MySQL databases.

So, you will need to completely rewrite the code if you want to switch to a different database.

However, PDO is able to work with 12 different databases, which makes the migration much simpler.

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