27 mei 2009

Creating, retrieving, updating & deleting records in PHP @ GAE

Google uses BigTable as the database in Google App Engine (GAE). This article uses the JDO layer with the Datastore Java API to create, retrieve, update & delete Database records in PHP at Google App Engine.

The starting point for this example is an GAE project in Eclipse with PHP, see this blog posting to set that up.

First create a java class for setting up the database connection, save below file as src/database/connection.java in your Eclipse GAE project.
package database;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;

public final class connection {
private static final PersistenceManagerFactory pmfInstance =

private connection() {}

public static PersistenceManagerFactory get() {
return pmfInstance;
Create a Java class with JDO annotations for every database table you want to use, below the one as used in this article, save it as src/database/person.java in your Eclipse GAE project.
package database;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class person {

@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long person;

private String firstName;

private String lastName;

public Long getPerson() {
return person;

public String getFirstName() {
return firstName;

public String getLastName() {
return lastName;

public void setFirstName(String in) {
this.firstName = in;

public void setLastName(String in) {
this.lastName = in;

Above Java code has the same functionality as below SQL code (please make no nasty comments about the java folks)
firstName TEXT,
lastName TEXT,
PRIMARY KEY (person)
Now we are ready to create PHP scripts that uses GAE BigTable as database! Below the PHP source that will be used as a template for every example.

echo "<pre>";

// Import the java code for setting up a database connection.
import database.connection;

// Import the table definition class
import database.person;

// Setup a database connection.
$connection = connection::get()->getPersistenceManager();

..... example code goes here .....

// Close the database connection.

echo "</pre>";

Creating records in the database can be done with below PHP code.
$record = new person;



$key = $record->getPerson(); // Not the record but the field person that is primary key

echo "Added 'John Doe' with number $key in the database \n";

$record = new person;



$key = $record->getPerson(); // Not the record but the field person that is primary key

echo "Added 'Jane Doe' with number $key in the database \n";
Retrieving records from the database, that can be done with below PHP script.
$query = $connection->newQuery("select from database.person");
$results = $query->execute();

foreach($results as $record) {
echo "Key: " . $record->getPerson();
echo " Name: " . $record->getFirstName() . " " . $record->getLastName() . "\n";
Above examples prints all records from the table, of course you can change that, use something like below
$query = $connection->newQuery("select from person where person == 5");
$query = $connection->newQuery('select from person where lastName <> "Doe"');
A Update example below.
$query = $connection->newQuery('select from database.person where firstName == "John"');
$results = $query->execute();

$record = $results[0];


The delete example:
$query = $connection->newQuery('select from database.person where firstName == "Jane"');
$results = $query->execute();


See here for a proof of concept how you can use 'normal' SQL in PHP at GAE

Sources from this article

4 opmerkingen:

Mark Essel zei

I had a problem accessing the functions. I moved the database package into the other package I had setup for a eclipse/java project but it's not recognizing the java functions. Is there somewhere else I have to define the classes (I've had some success with php/quercus on the gae for java).

Radhika Reddy zei


Can you please mention the changes in a hierarchial tree form so that we can understand which file to be placed at what location.
here changes to be made in servlet are not mentioned

I want JDO on app engine java to send a string to php so that it can store in mysql database

yasir zei

I am getting the following error while executing the neWquery function:
$query = $this->connection->newQuery("select from sinica.googlefileservice.server.datastore.tbl_corporation where id == 222 and corp_contact_person == 'XYZ'");

com.caucho.quercus.QuercusException: org.datanucleus.jdo.JDOQuery.execute: Portion of expression could not be parsed: and corp_contact_person == 'XYZ'

Is there any other finction to fecth the reacord based on mutiple condion in where clause?

Anoniem zei

I've read a few good stuff here. Certainly price bookmarking for revisiting. I surprise how much attempt you put to create one of these magnificent informative website.

my web page - special compilation