|
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | 17 |
|
18 | | -# [START gae_cloudsql_example] |
19 | | -// Connect to CloudSQL from App Engine. |
20 | 18 | $dsn = getenv('CLOUDSQL_DSN'); |
21 | 19 | $user = getenv('CLOUDSQL_USER'); |
22 | 20 | $password = getenv('CLOUDSQL_PASSWORD'); |
| 21 | + |
| 22 | +// Ensure the required environment variables are set to run the application |
23 | 23 | if (!isset($dsn, $user) || false === $password) { |
24 | 24 | throw new Exception('Set CLOUDSQL_DSN, CLOUDSQL_USER, and CLOUDSQL_PASSWORD environment variables'); |
25 | 25 | } |
26 | 26 |
|
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 | +// |
28 | 43 | $db = new PDO($dsn, $user, $password); |
29 | 44 |
|
30 | 45 | // 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(); |
39 | 49 |
|
40 | 50 | // Insert a new row into the guestbook on POST |
41 | 51 | if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
42 | 52 | $stmt = $db->prepare('INSERT INTO entries (guestName, content) VALUES (:name, :content)'); |
43 | | - $result = $stmt->execute([ |
| 53 | + $stmt->execute([ |
44 | 54 | ':name' => $_POST['name'], |
45 | 55 | ':content' => $_POST['content'], |
46 | 56 | ]); |
47 | | - if (false === $result) { |
48 | | - print("Error: " . $stmt->errorInfo()[2]); |
49 | | - } |
50 | 57 | } |
51 | 58 |
|
52 | | -// Show existing guestbook entries. |
| 59 | +// Query existing guestbook entries. |
53 | 60 | $results = $db->query('SELECT * from entries'); |
54 | 61 |
|
| 62 | +# [END gae_cloudsql_example] |
55 | 63 | ?> |
56 | | -<?php ?> |
| 64 | + |
57 | 65 | <html> |
58 | 66 | <body> |
59 | 67 | <?php if ($results->rowCount() > 0): ?> |
|
71 | 79 | </form> |
72 | 80 | </body> |
73 | 81 | </html> |
74 | | -<?php # [END gae_cloudsql_example]?> |
|
0 commit comments