Question

I'm trying to create a program that shows the emails of teachers. The name and emails of these teachers are in a database.

I took the sample code from Google and modified it, but it does not seem to work. I am also unable to resolve the error, as the message given is "Unknown error". How should I go about to debug / fix this error?

I have restarted the sql server, and tried tweaking the code in many many ways, but it doesn't seem to want to give me my result :( I have added the gae app to the applications list for the database as well.

The relevant code snippet is found here:

For security, I have changed the code so that the IP is 111.111.111.111 and the database is db.

<h2>Teachers:</h2>
  <?php
  // Create a connection.

  $db = null;
  if (isset($_SERVER['SERVER_SOFTWARE']) &&
  strpos($_SERVER['SERVER_SOFTWARE'],'Google App Engine') !== false) {
    // Connect from App Engine.
    try{
       $db = new PDO('mysql:host=111.111.111.111:3306;dbname=db', 'root', 'password');
    }catch(PDOException $ex){
        die(json_encode(
            array('outcome' => false, 'message' => 'Unable to connect.', 'error' => $ex->getMessage())
            )
        );
    }
  }

  try {
    // Show existing guestbook entries.
    foreach($db->query('SELECT * from db.teacher') as $row) {
            echo "<div><strong>" . $row['title'] . " " .$row['name'] . "</strong> has email <br> " . $row['email'] . "</div>";
     }
  } catch (PDOException $ex) {
    echo "An error occurred.";
  }
  $db = null;
  ?>

When I go to the website, this is what I am greeted with:

Teachers:

{"outcome":false,"message":"Unable to connect.","error":"SQLSTATE[HY000] [2002] Unknown error 4294967295"}

Thank you!!

Was it helpful?

Solution

You don't need to specify the IP address

$db = new pdo('mysql:unix_socket=/cloudsql/<your-project-id>:<your-instance-name>;dbname=<database-name>',
  'root',  // username
  ''       // password
  );

https://developers.google.com/appengine/docs/php/cloud-sql/

If your app and DB are connected properly (you have given permission to that GAE app to access that Google SQL database etc) then it should "just work".

If you want to connect by IP you have to go in to the instance and give it an IP address manually.

OTHER TIPS

When connecting from a GAP authorized app, you don't need to use password. Did you try this? Connect without password.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top