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.
Index
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.
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.
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";
?> 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";
?> 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:
Parameters
Read More: Create Table in MySQL
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 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…