0

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!!

pipng13579
  • 180
  • 10

2 Answers2

2

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.

Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36
  • Hi @Paul, I previously tried the above but I also got this error. I wrote `$db = new pdf('mysql:unix_socket=/cloudsql/appid:instance;dbname=guestbook', 'root', 'password');` Where appid is the name of the app (from appid.appspot.com) – pipng13579 May 10 '14 at 13:53
  • can you connect to the DB with the credentials you have on the command line? – Paul Collingwood May 10 '14 at 13:54
  • Sorry, how do you do this? I can connect to the sql instance using the mysql gui. Is this similar enough? – pipng13579 May 10 '14 at 14:18
  • After leaving the program for awhile, the next time I ran it, I got an error stating that access was denied (a more useful error!). This was solved by removing the password (http://stackoverflow.com/questions/20896237/app-engine-java-servlet-does-not-connect-to-cloud-sql). I guess something or other was a little temperamental. Thanks @Paul for your help :) – pipng13579 May 10 '14 at 16:15
  • Note that some commands require a `FLUSH PRIVILEGES` or a server restart to take effect. Reference: http://dev.mysql.com/doc/refman/5.5/en/privilege-changes.html – Razvan Musaloiu-E. May 11 '14 at 16:30
1

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

Tiago Gouvêa
  • 15,036
  • 4
  • 75
  • 81