We use this code and store in file name config.php. You can use different file name. Call this file from all scripts ( take care of path based on your file location ) by using this line
require "config.php";// Database connection
Now use the $connection as connection object in your script. Here is the code for config.php file.
<?Php
$host = "localhost";
$database = "sql_tutorial";// Change your database name
$username = "userid"; // Your database user id
$password = ""; // Your password
//error_reporting(0);// With this no error reporting will be there
///// Do not Edit below //////
$connection=mysqli_connect($host,$username,$password,$database);
if (!$connection) {
echo "Error: Unable to connect to MySQL.<br>";
echo "<br>Debugging errno: " . mysqli_connect_errno();
echo "<br>Debugging error: " . mysqli_connect_error();
exit;
}
?>
Here the variables $host is name of the MySQL server host or ip address ( 'localhost' or '127.0.0.1')
We will use $connection object in our script to access our database.
Using try - catch code block to handle error in connection
<?php
$host = 'localhost'; // MySQL host address
$username = 'root'; // User id to login
$password = 'test'; // Password for user id
$database = 'my_tutorial'; // Database name
try {
// Establish MySQLi connection
$connection = new mysqli($host, $username, $password, $database);
// Check for connection error
if ($connection->connect_error) {
// Throw exception if connection fails
throw new Exception('Connection failed: ' . $connection->connect_error);
}
} catch (Exception $e) {
// Handle connection error
echo 'Error: ' . $e->getMessage();
echo "<br>Debugging errno: " . mysqli_connect_errno();
echo "<br>Debugging error: " . mysqli_connect_error();
} finally {
echo "Connection successful!";
}
?>
$connection->connect_error
The connect_error property is set by the MySQLi extension when an error occurs during the attempt to establish a connection to the database. It will contain a string describing the error if the connection fails, or it will be NULL if no error occurred.
Here are the list of error messages if any of the input details or credentials are wrong.
If password is wrong. Access denied for user 'root'@'localhost' (using password: YES)
If userid is wrong. Access denied for user 'root1'@'localhost' (using password: YES)
If Database is wrong. Unknown database 'my_tutorial1'
If host address is wrong. php_network_getaddresses: getaddrinfo failed: No such host is known.
throw new Exception('Connection failed: ' . $connection->connect_error);
is used to throw a custom exception in the event that a connection to the MySQL database fails. Here’s how it works:
Throwing an Exception:throw new Exception(...) is a way to raise an error condition intentionally. When this is encountered, the code stops execution at that point and the control is passed to the catch block (if available).
An Exception object is created with the message passed inside the parentheses.
Custom Error Message:
'Connection failed: ' is a string that is prepended to the actual error message from the database. $connection->connect_error fetches the error message generated during the MySQLi connection attempt. This error message gives details about why the connection failed (e.g., wrong username, password, or database name).This is same as explained above. Combining the Message:
The full error message is created by concatenating the custom message ('Connection failed: ') with the actual error stored in $connection->connect_error.
This provides a clear explanation of what went wrong during the connection attempt.
Google Cloud & MySQL connection using MySQLi
<?Php
$host = "34.68.103.244";
$database = "my_tutorial"; // Change your database name
$username = "root-plus2net"; // Your database user id
$password = "*********"; // Your password
//error_reporting(0);// With this no error reporting will be there
///// Do not Edit below //////
$connection=mysqli_connect($host,$username,$password,$database);
if (!$connection) {
echo "Error: Unable to connect to MySQL.<br>";
echo "<br>Debugging errno: " . mysqli_connect_errno();
echo "<br>Debugging error: " . mysqli_connect_error();
exit;
}
//////Sample script to display record ///
$q="SELECT * FROM student WHERE id=3";
// Generate resultset
$result_set = $connection->query($q);
$row=$result_set->fetch_array(MYSQLI_NUM);
echo $row[0],$row[1],$row[2],$row[3];
$result->free;
?>