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.
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;
?>