MySQL tidbits: IF statement

A useful (but seemingly underused) SQL keyword is the IF statement.

The usage is as follows:

SELECT IF(expr1, expr2, expr3);

If expr1 evalues to true, then expr2 is executed, otherwise, expr3 is. This is useful for doing conditional queries inside the SQL without having to put logic in your script/program.

SELECT IF( (SELECT name FROM people) = 'john'), (SELECT age FROM people WHERE name = 'john') , 'no age');

The above statement will return the age of the user ‘john’ if he exists in the people table. If not the query will return ‘no age.’
The caveat is that expr2 and expr3 must evaluate to a single value. The braces around expr2′s SELECT statement are required.

Flash / ActionScript + AMF + Zend Framework mini-How-To

There are times when you need your Flash applet to fetch data from an external source. The simplest way to do that with a PHP backend is a combination of Zend Framework’s AMF module and Flash’s built in AMF remoting feature (Action Message Format).

The steps to go about doing that is to have the client side flash component, and a server side PHP component with Zend Framework installed.
On the flash side, you create a netconnection object, assign the gateway as the URI of your web service (http://example.com/services/amf.php in this case). You specify a responder and set it to listen to any events. In the example below, I use JSON to encode the objects, but that’s optional, I just happened to be dealing with JSON objects at the time.

Source example follows:
Continue reading

Java Applet with PHP XML-RPC web service

In the past, I needed to write Java applets that interacted with a PHP backend for data fetch. One method I used was to implement XML-RPC. The XML-RPC standards. On the PHP side, Zend Framework has a pretty good XML-RPC server / client class built in. See Zend Framework XML-RPC. On the Java side you can implement the XML-RPC client as such.
Continue reading