Hello friends, I have seen that many people facing difficulty during connectivity of PHP with SQL, So today we see how we will resolve this problem easily.
Note:- Please read the whole blog to avoid any inconvenience.
Step 1 :
Create a file or open your favorite notepad generally for programming or scripting developers use notepad++ you can use any.Step 2 :
Trigger your server or localhost if you have a XAMPP then you need to press Apache or MYSQL buttons until it will turn into green.Step 3 :
Write commands like below
<?php
$servername = "localhost";
$username = "root";
$password = "";
$connect = mysqli_connect($servername , $username, $password);
if(!$connect)
{
die("Connection Failed".mysqli_connect_error());
}
echo "Connected Successfully";
mysqli_close($connect);
?>
- Step 4: Let's understand the above code first we all know we need to write our PHP logic under <?php ---- ?> tag. So in this module, we simply declaring 3 variables that are mandatory during connectivity.
$servername = "localhost";
$username = "root";
$password = "";
variables like servername, username and password.
This variable is used to storing our XAMPP credentials make sure you enter your credentials according to your XAMPP data.
After that, We store a function that names
mysqli_connect();
in a variable let's say $connect
so we storing a predefined function provided by PHP in a variable to use easily and which also helps for validation.
- Step 5: Here we connected with our SQL server, We just need to validate ourself For validating or Ensuring we use conditional statement that provides boolean value
if(!$connect)
{
die("Connection Failed".mysqli_connect_error());
}
echo "Connected Successfully";
Here we in If statement we checking if our $connect variables is negative (i.e, False) so simply call die function and write a message according to your convenience and also concatenate mysqli_connect_error() function for more satisfation using . operator which is used for concatenate in PHP.
- Step 6: Finally we closing our server using mysqli_close() for better practices.
I hope you learned from my post if any query visit my GitHub page for discussion.
ItsFRZ
And if you found my work is helpfull please do share
Top comments (0)