PHP MySQLi Connect

In this article, we will learn what is 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.

In order to store or retrieve the data inside a MySQL database, we first need to connect to the MySQL database server.  

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

It returns result if connection is established or 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 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 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 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 local server username is root.
  • passwordIt is optional and specifies MySQL password.
  • database_nameIt is database name where operation perform on data. It also optional.

Read More: Create Table in MySQL

Conclusion

In this article, we learnt all about mysqli_connect connection variable and how to apply it using various methods in PHP.

I hope you find 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