1
votes

I used the environment of the "hello world" app engine example by google to create my own php app. Then i configured the existing app.yaml file to this:

runtime: php55
api_version: 1

handlers:
- url: /
  script: index.php

env_variables:
    MYSQL_DSN: mysql:dbname=sampledb;unix_socket=/cloudsql/sampleproject:europe-west3:sampleinstance
    MYSQL_USER: root
    MYSQL_PASSWORD: admin

My index.php is a typical login page. I want to load user data from my cloud sql to check, whether the user is allowed to enter the next page or not. The next page is a dashboard which includes company data. I also want to load this data from my cloud sql databse.

That's my php code to read the data (i copied it from the google bookshelf tutorial, because i am completly new into these things..)

  <?php
    $dsn = getenv('MYSQL_DSN');
    $user = getenv('MYSQL_USER');
    $password = getenv('MYSQL_PASSWORD');
    if (!isset($dsn; $user) || false=== $password) {
      throw new Exception('Set MYSQL_DSN');
      }
    $db = new PDO($dsn, $user, $password);

    $statement = $db->prepare("SELECT clicks from display_inbox");
    $statement->execute();
    $all = $statement->fetchAll();

    foreach ($all as $data) {
      echo $data["clicks"], "<br>";
    }
  ?>

It's not a specific query,i just want to check the connection. After deploying my app i get the following result:

HTTP-500 error: sampleproject.appspot.com is currently unable to handle this request.

I simply don't get if it's my app.yaml or my php code. Would be very helpful if anybody has an example code for a app.yaml file and a typical query to read the cloud sql data.

1
can you edit your post by adding the exact error you receive after deployment?Tudormi
by the way: i authenticated my ip and acitvated all APIsToby
Follow this example, which is exactly App Engine Flexible php - CloudSQL. There you have an app.yaml and a working query. Are you running the command gcloud app deploy [YAML FILE] and immediatly after you get the 500 error? Or the deployment is successful but then when you perform an HTTP request to your app, it responds with that 500 (all the time)?Tudormi
i followed the example and tried to deploy the sample app. But it's still not working.. i can deploy the app, but when i perfom an http request, it responds with 500..Toby
Are you sure that the title is leading in the wrong direction? How do you know that it's the connection to Cloud SQL the one that is failing? The only information you have is that HTTP-500 error: sampleproject.appspot.com is currently unable to handle this request. Can you share a log entry from Stackdriver logging? One that would reflect a failed request.Tudormi

1 Answers

0
votes

You have some syntax errors in your app.yaml and index.php files

Try this:

app.yaml:

runtime: php55
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: index.php

# [START env]    
env_variables:
    MYSQL_DSN: mysql:unix_socket=/cloudsql/sampleproject:europe-west3:sampleinstance;dbname=sampledb
    MYSQL_USER: root
    MYSQL_PASSWORD: admin
# [END env]

index.php:

  <?php
    $dsn = getenv('MYSQL_DSN');
    $user = getenv('MYSQL_USER');
    $password = getenv('MYSQL_PASSWORD');
    if (!isset($dsn, $user) || false === $password) {
      throw new Exception('Set MYSQL_DSN');
      }
    $db = new PDO($dsn, $user, $password);

    $statement = $db->prepare("SELECT clicks from display_inbox");
    $statement->execute();
    $all = $statement->fetchAll();

    foreach ($all as $data) {
      echo $data["clicks"]."<br>";
    }
  ?>