PHP MySQLi Connect

In this article, we will learn about mysqli_connect and how to use mysqli_connect to use a MySQL database with PHP.

We can easily modify, view, or manage the tables created in the MySQL database using MySQLi functions. We have to create a connection variable using mysqli_connect in PHP which connects with MySQL.

MySQLi Connect in PHP

In this unit, we learn about several methods to connect to a MySQL database using PHP.

To store or retrieve the data inside a MySQL database using a MySQL query, we first need to connect to the MySQL database server.  

PHP mysqli_connect() function is used to connect to the MySQL database.

It returns a result if a connection is established or else, returns Null.

Different ways of working with MySQL and PHP.

  • MySQLi (object-oriented)
  • MySQLi (procedural)
  • PDO 

Read Also: PHP MySQL Database

The PDO extension is more portable and supports different databases, MySQLi extension as the name suggests supports the MySQL database only.

MySQLi extension provides an easier way to connect to, and execute queries on a MySQL database server.

Both PDO and MySQLi offer an object-oriented API, but MySQLi also offers a procedural API which is relatively easy for beginners to understand.

MySQLi Procedural Method

Syntax

$link = mysqli_connect("hostname", "username", "password", "database");

The PHP script for connecting the MYSQL database using mysqli procedural 

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

MySQLi Object-oriented Method

Syntax

$mysqli = new mysqli("hostname", "username", "password", "database");

The PHP script for connecting the MySQL database using MySQL (Object-oriented way)

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully";

?>

MySQLi PDO Method

Syntax

$pdo = new PDO("mysql:host=hostname;dbname=database", "username", "password");

The PHP script for connecting the MySQL database using MySQL (PDO way)

<?php

 $servername = "localhost";

 $username = "username";

 $password = "password";

 try {

   $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);

   // set the PDO error mode to exception

  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

   echo "Connected successfully";

 } catch(PDOException $e) {

   echo "Connection failed: " . $e->getMessage();

 } 

?> 

Note:

  • The default username for the MySQL database server is ‘root’, and there is no password.
  • To prevent databases from intrusion and unauthorized access, we should set a password for MySQL accounts.

Parameters

  • localhost: The localhost is optional and specifies the hostname or IP address.
  • usernameIt is optional and specifies MySQL username. In the local server, the username is the root.
  • passwordIt is optional and specifies MySQL password.
  • database_nameIt is the database name where we perform operations on data. It is optional.

Read More: Create Table in MySQL

Conclusion

In this article, we learned all about the mysqli_connect connection variable and how to apply it using various methods in PHP. Before working with databases, we suggest you refer to our PHP Tutorial section for learning the basics.  

We hope you found this article helpful 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Read previous post:
PHP MySQL Database
PHP MySQL Database

MySQL is one of the popular relational database systems. With PHP, we can connect and manipulate MySQL databases. MySQL Database...

Close