1

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
Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
Curvegraph
  • 850
  • 1
  • 10
  • 17
  • Have you checked this case [https://stackoverflow.com/questions/32044160/google-cloud-sql-with-php]? Here [https://gist.github.com/jsimonweb/7533007] there is a good example on how to connect to a Google Cloud SQL database. – gr7 May 02 '18 at 10:36

0 Answers0