如何连接php和cloudsql

nwnhqdif  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(241)

我试着像下面这样连接cloudsql,但它不起作用。有谁能帮我用php连接cloudsql吗?
我试了所有的答案,但都找不到解决办法

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;
    }

}

这里我添加了我得到的错误

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题