When you are using php you first need to connect to the mysqli database by using simple php query.
Example:
[php]<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "databasename";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>[/php]
Code Explanation:
In the above php query, we have first declared the variables $servername, $username, $password, $database
. This is the login information to connect the mysqli database.
To connect we will call the php function mysqli()
$conn = new mysqli($servername, $username, $password, $database);
We created a variable called $conn
and trying to connect the database by using our variable details.
On next step, we are verifying the connection by using if statement.
[php]if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} [/php]
In the if statement we are checking if the connection is successful or not by using the connect_error
condition. if it fails to connect then our die()
error function will be a call to show the message “Connection failed
” and it will show the error message.