Posts tonen met het label PHP. Alle posts tonen
Posts tonen met het label PHP. Alle posts tonen

4 september 2015

Champ: Apache, MySQL & PHP from source on your Chromebook




Champ = CH(romebook) A(pache) M(ySQL) P(hp)


Lamp is pretty popular and a well know term in the Web Developers world, Wamp also exists, and I have used Xampp with pleasure for many years.

But this blog posting is about the real champion: It's about Champ !!!

Ch = Chromebook
a  = Apache
m  = MySQL
p  = PHP

Turn your Chromebook into a low cost Apache/PHP/MySQL development machine, run Apache/PHP/MySQL offline, develop LAMP websites offline.

It started because I wanted to test my own PHP apps with the new PHP 7.0 version that was in in that time in beta stage. I was wondering if I could use my Chromebook for this. YES, that was possible !

( ok ok, the term Champ is technically wrong, I have used Debian in a chroot environment that is setup by Crouton, so it is still Lamp, but the term Champ is just to good not to use it )

..... Read more .....

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 =
JDOHelper.getPersistenceManagerFactory("transactions-optional");

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 {

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

@Persistent
private String firstName;

@Persistent
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)
CREATE TABLE person (
person BIGINT AUTO_INCREMENT ,
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.
<?php

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.
$connection->close();

echo "</pre>";

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

$record->setFirstName('John');
$record->setLastName('Doe');

$connection->makePersistent($record);

$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;

$record->setFirstName('Jane');
$record->setLastName('Doe');

$connection->makePersistent($record);

$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];

$record->setFirstName("Lucky");
$record->setLastName("Luke");

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

$connection->deletePersistentAll($results);

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

Sources from this article
..... Read more .....

25 mei 2009

Using PHP on GAE


Google is now supporting 2 languages for Google App Engine: Phyton and Java. This article shows how you can use PHP on Google App Engine.

Running PHP on GAE is possible by using Quercus from Caucho, Quercus implements PHP in Java, see below URL's about running Quercus on GAE>
Assuming you have a Google App Engine website running with Java enabled and also localy installed the GAE Java SDK and the Google Eclipse plugin.

Assuming also that you did the Java Getting Started tutorial to get hands-on experience with the GAE Java SDK and the Google Eclipse Plugin.

Then you can also get PHP running on GAE with below steps.

1) Create a Google Web Application inside Eclipse

Optional: De-select GWT on the "New Web Application project" wizzard.

2) Copy resin.jar from Resin 4.0 to WEB-INF/lib

Add resin.jar to the java build path: Right-click on the project-name, select 'properties' then 'Java build path' and the add resin.jar under 'Libraries'

3) Modify war/WEB-INF/web.xml so that it contains (also) below content
<welcome-file-list>
<welcome-file>index.php</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>quercus</servlet-name>
<servlet-class>com.caucho.quercus.servlet.GoogleQuercusServlet<servlet-class>
</servlet>

<servlet-mapping>
<url-pattern>*.php</url-pattern>
<servlet-name>quercus</servlet-name>
</servlet-mapping>
4) Add below lines to war/WEB-INF/appengine-web.xml
<static-files>
<exclude path="/**.php" />
</static-files>

<resource-files>
<include path="/**.php" />
</resource-files>
5) Create the war/index.php file.
<?php 

phpinfo();

?>
6) Run the development server.

7) Surf to: http://localhost:8080/

..... Read more .....

Using the GAE log with PHP


With a very small java source you can use the GAE log with PHP. You can see this log on the console tab in Eclipse, or with the GAE Dashboard. It is also possible to download the logs from GAE with the appcfg.sh script.

This article shows you how you can use this log in your PHP scripts running on Google App Engine.

The usage in your PHP scripts is:
logInfo("I just want to say Hello!");
logWarning("Not good, xxx is not supported");
logSevere("Big big troubles");
First add resin.jar to the build path for your GAE project in Eclipse with properties -> Java build path -> Libraries

Below the Java code, save it as src/gae/php_log.java in your GAE project in Eclipse.
package gae;

import java.util.logging.Logger;
import com.caucho.quercus.module.AbstractQuercusModule;

public class php_log extends AbstractQuercusModule {

private static final Logger log = Logger.getLogger(php_log.class.getName());

public void logInfo(String message) { log.info(message); }
public void logWarning(String message) { log.warning(message); }
public void logSevere(String message) { log.severe(message); }

}
To make Quercus aware of your new log fuctions, add below line to the file src/META-INF/services/com.caucho.quercus.QuercusModule (create this file if it not yet exists)
 gae.php_log
To activate the INFO severity (default it is at WARNING severity), add below line to the war/WEB-INF/logging.properties file.
 gae.php_log.level=INFO
See quercus: java and php integration for more information how to write your own PHP functions with Java for Quercus PHP on GAE.
..... Read more .....

Sending email with PHP at GAE

Currently sending email from PHP scripts on Google App Engine is not working. Most likely this will be possible in the future again when the nice folks of Caucho adjust Quercus to the GAE sandbox.

This article shows how to use a small Java program for sending email from PHP at Google App Engine.

With using a small java source you can send email from your PHP scripts at GAE with below line:
 gaeMail('me@gmail,com', 'someone@inter.net', 'Subject', 'Hi, how are you?');
First add resin.jar to the build path for your GAE project in Eclipse with properties -> Java build path -> Libraries

Below the Java code, save it as src/gae/email.java in your GAE project in Eclipse.
package gae;

import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.caucho.quercus.module.AbstractQuercusModule;

public class email extends AbstractQuercusModule {

public void gaeMail(String from, String to, String subject, String body)

throws java.io.IOException {

Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);

try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText(body);
Transport.send(msg);
} catch (AddressException e) {
// ...
} catch (MessagingException e) {
// ...
}

}

}
To make Quercus aware of your new email fuction, add below line to the file src/META-INF/services/com.caucho.quercus.QuercusModule (create this file if it not yet exists)
 gae.email
See quercus: java and php integration for more information how to write your own PHP functions with Java for Quercus PHP on GAE.
..... Read more .....

24 mei 2009

SQL to JDO for PHP at GAE


Started a project that makes it possible to use 'normal' SQL like below in PHP scripts at Google App Engine.
sql("insert into demo (field1, field2) values(1, 'Yes it works')");

$rows = sql("select demo, field1, field2 from table order by demo desc");

sql("update demo set field1 = $field1 where demo = $demo");

sql("delete from demo where demo = $demo");

There is now a new version, see below blog postings about it.

pQg- Use SQL in PHP scripts at GAE

How to use pQg
..... Read more .....