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

Questions:
    <?php

//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->setAuthConfig('client.json');
//$client->setLoginHint('[email protected]');

$servername = "127.0.0.1";
$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(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
); 

try 
{
    // Coonection establishment
    $conn = new PDO($dsn, $username, $password, $options);
    //$conn = new PDO($dsn, $username);
    //echo $conn;
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";



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

        // 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: