Wednesday, April 23, 2025

MySQLi Server Connection

server_connection.php


<?php
//Config

$server = "localhost";
$user = "root";
$password = "";

//Establishing a Connection to MySQL Server
$connection = mysqli_connect($server, $user, $password);

//Check Connection
if (!$connection) {
	echo "Check Config, or is Server Alive ?";
} else {
	echo "Connection Successfull";
}

?>
  • $server = "localhost";: This line defines a variable $server and sets its value to "localhost". This variable holds the hostname of the MySQL server.
  • $user = "root";: This line defines a variable $user and sets its value to "root". This variable holds the username to access the MySQL server.
  • $password = "";: This line defines a variable $password and sets its value to "". This variable holds the password to access the MySQL server.
  • $connection = mysqli_connect($server, $user, $password);: This line establishes a connection to the MySQL server using the mysqli_connect() function, which is provided by the MySQLi extension. It takes three parameters - $server, $user, and $password - which contain the hostname, username, and password to connect to the server. The function returns a connection object if the connection is successful, otherwise it returns false. The connection object is assigned to a variable $connection.
  • if (!$connection) {: This line starts an if statement that checks whether the connection was successful or not. The ! operator negates the value of $connection, so if $connection is false, the condition evaluates to true.
  • echo "Check Config, or is Server Alive ?";: This line prints an error message to the screen if the connection was not successful.
  • } else {: This line marks the beginning of the else block, which is executed if the connection was successful.
  • echo "Connection Successfull";: This line prints a success message to the screen if the connection was successful.
  • }: This line marks the end of the if-else block.

server_connection_with_dedicated_check.php


<?php
//Config

$server = "localhost";
$user = "root";
$password = "";

//Establishing a Connection to MySQL Server
$connection = mysqli_connect($server, $user, $password);

//Check Connection
if (!$connection) {
	die("<h2>Total Fail</h2> " . mysqli_connect_error());
} else {
	echo "Connection Successfull";
}

?>
  1. if (!$connection) {: This line starts an if statement that checks if the connection was unsuccessful. The ! operator is used to check if the value of $connection is false.

  2. die("<h2>Total Fail</h2> " . mysqli_connect_error());: This line prints an error message and stops the execution of the script if the connection was unsuccessful. The die() function is used to print the error message and exit the script. The mysqli_connect_error() function is used to retrieve the error message from the most recent MySQL connection error.

  3. echo "Connection Successfull";: This line prints a success message if the connection was successful. The echo statement is used to output the message to the web page.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...