Home » Php » php – how to connect to cloud sql of another project in gcp

php – how to connect to cloud sql of another project in gcp

Posted by: admin February 25, 2020 Leave a comment


//use Google_Client;

echo "<table style='border: solid 1px black;'>";
echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";

class TableRows extends RecursiveIteratorIterator {
    function __construct($it) {
        parent::__construct($it, self::LEAVES_ONLY);

    function current() {
        return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";

    function beginChildren() {
        echo "<tr>";

    function endChildren() {
        echo "</tr>" . "\n";

//$client = new Google_Client();
//$client->setLoginHint('[email protected]');

$servername = "";
$username = $_POST["username"];
$password = $_POST["password"];
//$mydb = "xp_database";
//$port="3308"; inner-period-262304:asia-east2:xpserve-sqlinstance
//CLOUDSQL_DSN: "mysql:dbname=DATABASE;unix_socket=/cloudsql/inner-period-262304:asia-east2:xpserve-sqlinstance"
$dsn = 'mysql:dbname=xp_database;unix_socket=/cloudsql/inner-period-262304:asia-east2:xpserve-sqlinstance';

$options = array(

    // Coonection establishment
    $conn = new PDO($dsn, $username, $password, $options);
    //$conn = new PDO($dsn, $username);
    //echo $conn;
    // set the PDO error mode to exception
    echo "Connected successfully";

        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("SELECT custname FROM cust_master_table");

        // set the resulting array to associative
        $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
        foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
            echo $v;

    echo "</table>";

catch(PDOException $e)
    echo "Connection failed: " . $e->getMessage();
echo "hello db9";

I have created a sql instance in one project. My app is in another project in gcp. The app is a php app. Iam not able to retrieve the records from the website. I get the following message in logs

nginx: [warn] the “user” directive makes sense only if the master process runs with super-user privileges, ignored in /tmp/google-config/nginx.conf:3

Please tell me how to go about this.

How to&Answers: