database php connection code example
Example 1: php sql connection
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn= mysqli_connect($servername,$username,$password,$dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected Successfully.";
?>
Example 2: database connectivity in php
<?php
$serve="localhost";
$user="root";
$password="";
$db="cart_system";
$conn=mysqli_connect($serve,$user,$password,$db);
if ($conn) {
echo "connection success";
}else
{echo "connection unsuccess";
}
?>
Example 3: how to connect to a database in php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>