PHP connect to DB

PHP connect to DB

Example (MySQLi procedural)

mysqli_connect function is used for connection string which has 3 parameters and mysqli_connect_error function is used to display particular MySQL error.

<?php
$servername = “localhost”;
$username = “root”;
$password = “”;

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

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

Example (MySQLi Object-Oriented)

<?php
$servername = “localhost”;
$username = “root”;
$password = “”;

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

// Check connection
if (mysqli_connect_error()) {
  die(“Database connection failed: ” . mysqli_connect_error());
}
echo “Connected successfully”;
?>

Post Your Comments & Reviews

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

*

*