I tried to connect cloud-sql like below, but it doesn't work. Can anyone help me how to connect with Cloud sql using php ?
I tried all stack-overflow answers but I can'f find the solution
function connect() {
$hostname = null; //Defaults to mysqli.default_host
$username = "root";
$password = "";
$database = "db"; //Defaults to ""
$port = null; //Defaults to mysqli.default_port
$socket = "/cloudsql/Instance_connection_name";
// Connecting to mysql database
$this->conn =new mysqli($hostname, $username, $password, $database, $port, $socket);
// $mysqli->set_charset("utf8");
// Check for database connection error
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
// returing connection resource
return $mysqli;
}
}
Here I have added the error I got
Warning: mysqli::__construct(): (HY000/2002): No such file or directory in /app/projectname/include/DbConnect.php on line 26 Failed to connect to MySQL: No such file or directory