Skip to content

Commit 62f8414

Browse files
committed
better cloudsql samples for appengine php 7.2
1 parent d2c2390 commit 62f8414

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

appengine/php72/cloudsql/index.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,29 @@
1515
* limitations under the License.
1616
*/
1717

18-
$dsn = getenv('CLOUDSQL_DSN');
19-
$user = getenv('CLOUDSQL_USER');
20-
$password = getenv('CLOUDSQL_PASSWORD');
21-
2218
// Ensure the required environment variables are set to run the application
23-
if (!isset($dsn, $user) || false === $password) {
24-
throw new Exception('Set CLOUDSQL_DSN, CLOUDSQL_USER, and CLOUDSQL_PASSWORD environment variables');
19+
if (!getenv('CLOUDSQL_DSN') || !getenv('CLOUDSQL_USER') || false === getenv('CLOUDSQL_PASSWORD')) {
20+
die('Set CLOUDSQL_DSN, CLOUDSQL_USER, and CLOUDSQL_PASSWORD environment variables');
2521
}
2622

2723
# [START gae_cloudsql_example]
28-
// Create the PDO object to talk to CloudSQL. Use the following variables:
29-
//
30-
// $dsn = "mysql:dbname=DATABASE;unix_socket=/cloudsql/CONNECTION_NAME";
31-
// $user = 'YOUR_CLOUDSQL_USER';
32-
// $password = 'YOUR_CLOUDSQL_PASSWORD';
33-
//
3424
// If the unix socket is unavailable, try to connect using TCP. This will work
3525
// if you're running a local MySQL server or using the Cloud SQL proxy, for example:
3626
//
3727
// $ cloud_sql_proxy -instances=your-connection-name=tcp:3306
3828
//
3929
// This will mean your DSN for connecting locally to Cloud SQL would look like this:
4030
//
31+
// // for MySQL
4132
// $dsn = "mysql:dbname=DATABASE;host=127.0.0.1";
33+
// // for PostgreSQL
34+
// $dsn = "pgsql:dbname=DATABASE;host=127.0.0.1";
4235
//
43-
// For PostgreSQL examples see `app-postgres.yaml`
44-
//
36+
$dsn = getenv('CLOUDSQL_DSN');
37+
$user = getenv('CLOUDSQL_USER');
38+
$password = getenv('CLOUDSQL_PASSWORD');
39+
40+
// create the PDO client
4541
$db = new PDO($dsn, $user, $password);
4642

4743
// create the tables if they don't exist
@@ -64,7 +60,6 @@
6460
// Now you can use the PDOStatement object to print or iterate over the results:
6561
//
6662
// var_dump($results->fetchAll(PDO::FETCH_ASSOC));
67-
//
6863
# [END gae_cloudsql_example]
6964
?>
7065

0 commit comments

Comments
 (0)