PHP Connect MySQL Server

In this post, we will learn how to use PHP to connect to MySQL Server. Once connected to MySQL, you can use PHP code to perform CRUD operations on the database.

How To Connect PHP to MySQL

There are two main ways of connecting your PHP application to a MySQL Server:

  1. Using MySQLi extension
  2. Using PHP Data Objects

Let us explore.

PHP Connect MySQL – Using MySQLi Extension

The MySQLi or MySQL Improved extension is a PHP driver for MySQL databases. This extension comes with additional features and improvements over the old MySQL extension.

The MySQLi extension provides us with the mysqli_connect function, which allows us to connect to the MySQL database.

The function syntax is shown below:

mysqli_connect(
string $hostname = ini_get("mysqli.default_host"),
string $username = ini_get("mysqli.default_user"),
string $password = ini_get("mysqli.default_pw"),
string $database = "",
int $port = ini_get("mysqli.default_port"),
string $socket = ini_get("mysqli.default_socket")
): mysqli|false

The function accepts six arguments are described below:

  1. Hostname – this defines the hostname or IP address to the MySQL server. The function will default attempt to connect to localhost:3306 using TCP/IP protocol.
  2. Username – specifies the username to connect to the MySQL server.
  3. Password – sets the password for the specified username. This will help MySQL to authenticate and allow query execution.
  4. Database – this specifies which database to connect to. The function will connect to the default database if no value is specified.
  5. Port – sets the port for the MySQL server.
  6. Socket – defines the socket or name piped used to connect.

The function will then return an object representing the connection to the MySQL server. If the connection fails, the function returns false.

PHP MySQLi Connect to MySQL Server – OOP

The following shows the object-oriented code for connecting to the MySQL server using the mysqli_connect() function:

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "src";

$conn = new mysqli($hostname, $username, $password);
if ($conn -> connect_error) {
die("connection failed!". $conn->connect_error);
}
echo "connection successful";
?>

Once we run the previous code, we should get the output:

connection successful

The previous output shows the successful connection.

PHP MySQLi Connect to MySQL Server – Procedural

The following is the procedural code for connection to the MySQL server using the MySQLi extension:

<?php
$hostname = “localhost”;
$username = “root”;
$password = “”;
$database = “src”;

$conn = mysqli_connect($hostname, $username, $password, $database);
if (mysqli_connect_errno()) {
die(“connection failed”.myqli_connect_error());
}
echo “connection successful”;
?>

PHP Connect MySQL Server – PDO

To connect to the MySQL server using PHP Data Objects, run the code example as shown below:

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "src";

try {
$conn = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

echo "connection successful";
} catch (PDOException $e) {
echo "Connection failed". $e->getMessage();
}

Conclusion

This tutorial taught you how to connect to MySQL Server using PHP Data Objects and the PHP MySQLi extension. Feel free to explore the docs for extensive documentation.



from https://ift.tt/TjLK7qN

Post a Comment

0 Comments