Skip to content

Commit 6d6757e

Browse files
committed
misc php72 cloudsql fixes
1 parent f93d3b1 commit 6d6757e

File tree

5 files changed

+30
-23
lines changed

5 files changed

+30
-23
lines changed

appengine/php72/cloudsql/app.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ runtime: php72
1313
env_variables:
1414
# Replace USER, PASSWORD, DATABASE, and CONNECTION_NAME with the
1515
# values obtained when configuring your Cloud SQL instance.
16-
CLOUDSQL_USER: USER
17-
CLOUDSQL_PASSWORD: PASSWORD
16+
CLOUDSQL_USER:
17+
CLOUDSQL_PASSWORD:
1818
CLOUDSQL_DSN: "mysql:dbname=DATABASE;unix_socket=/cloudsql/CONNECTION_NAME"
1919
# [END gae_cloudsql_mysql_env]
2020

appengine/php72/cloudsql/index.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,53 @@
1515
* limitations under the License.
1616
*/
1717

18-
# [START gae_cloudsql_example]
19-
// Connect to CloudSQL from App Engine.
2018
$dsn = getenv('CLOUDSQL_DSN');
2119
$user = getenv('CLOUDSQL_USER');
2220
$password = getenv('CLOUDSQL_PASSWORD');
21+
22+
// Ensure the required environment variables are set to run the application
2323
if (!isset($dsn, $user) || false === $password) {
2424
throw new Exception('Set CLOUDSQL_DSN, CLOUDSQL_USER, and CLOUDSQL_PASSWORD environment variables');
2525
}
2626

27-
// Create the PDO object to talk to CloudSQL
27+
# [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+
//
34+
// If the unix socket is unavailable, try to connect using TCP. This will work
35+
// if you're running a local MySQL server or using the Cloud SQL proxy, for example:
36+
//
37+
// $ cloud_sql_proxy -instances=your-connection-name=tcp:3306
38+
//
39+
// This will mean your DSN for connecting locally to Cloud SQL would look like this:
40+
//
41+
// $dsn = "mysql:dbname=DATABASE;host=127.0.0.1";
42+
//
2843
$db = new PDO($dsn, $user, $password);
2944

3045
// create the tables if they don't exist
31-
$stmt = $db->prepare('CREATE TABLE IF NOT EXISTS entries ('
32-
. 'guestName VARCHAR(255), '
33-
. 'content VARCHAR(255))');
34-
$result = $stmt->execute();
35-
36-
if (false === $result) {
37-
exit("Error: " . $stmt->errorInfo()[2]);
38-
}
46+
$sql = 'CREATE TABLE IF NOT EXISTS entries (guestName VARCHAR(255), content VARCHAR(255))';
47+
$stmt = $db->prepare($sql);
48+
$stmt->execute();
3949

4050
// Insert a new row into the guestbook on POST
4151
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
4252
$stmt = $db->prepare('INSERT INTO entries (guestName, content) VALUES (:name, :content)');
43-
$result = $stmt->execute([
53+
$stmt->execute([
4454
':name' => $_POST['name'],
4555
':content' => $_POST['content'],
4656
]);
47-
if (false === $result) {
48-
print("Error: " . $stmt->errorInfo()[2]);
49-
}
5057
}
5158

52-
// Show existing guestbook entries.
59+
// Query existing guestbook entries.
5360
$results = $db->query('SELECT * from entries');
5461

62+
# [END gae_cloudsql_example]
5563
?>
56-
?>
64+
5765
5866
5967
if ($results->rowCount() > 0): ?>
@@ -71,4 +79,3 @@
7179
7280
7381
74-
# [END gae_cloudsql_example]?>

appengine/php72/cloudsql/phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
-->
17-
<phpunit>
17+
<phpunit bootstrap="./vendor/autoload.php">
1818
<testsuites>
1919
<testsuite name="AppEngine for PHP 7.2 Cloud SQL test">
2020
<directory>testdirectory>

appengine/php72/cloudsql/test/DeployMySQLTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function beforeDeploy()
4848
|| (!$database = getenv('CLOUDSQL_DATABASE'))
4949
|| false === $password = getenv('CLOUDSQL_PASSWORD')) {
5050
self::markTestSkipped('Set the CLOUDSQL_CONNECTION_NAME_MYSQL, CLOUDSQL_USER'
51-
. 'CLOUDSQL_DATABASE, and CLOUDSQL_PASSWORD environment variables');
51+
. ' CLOUDSQL_DATABASE, and CLOUDSQL_PASSWORD environment variables');
5252
}
5353

5454
$tmpDir = FileUtil::cloneDirectoryIntoTmp(__DIR__ . '/..');

appengine/php72/cloudsql/test/DeployPostgresTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function beforeDeploy()
4848
|| (!$database = getenv('CLOUDSQL_DATABASE'))
4949
|| false === $password = getenv('CLOUDSQL_PASSWORD')) {
5050
self::markTestSkipped('Set the CLOUDSQL_CONNECTION_NAME_POSTGRES, CLOUDSQL_USER'
51-
. 'CLOUDSQL_DATABASE, and CLOUDSQL_PASSWORD environment variables');
51+
. ' CLOUDSQL_DATABASE, and CLOUDSQL_PASSWORD environment variables');
5252
}
5353

5454
$tmpDir = FileUtil::cloneDirectoryIntoTmp(__DIR__ . '/..');

0 commit comments

Comments
 (0)