connect db mysql php code example
Example 1: php connect to mysql
$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";
Simplified
$conn = mysqli_connect('localhost', 'username', 'password');
$database = mysqli_select_db($conn, 'database');
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: $conn php
<?php
$user = "username";
$pass = "password";
$host = "host";
$dbdb = "database";
$conn = new mysqli($host, $user, $pass, $dbdb);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>