A not so new feature of PHP which dates back to version 5.0 is PDO (PHP Data Objects).  PDO is a data-access abstraction layer that can access several database engines whilst maintaining the same set of functions.  I’m surprised I hadn’t discovered PDO earlier considering its been around way before I knew PHP.

As part of my project, my sponsor has asked me to look into the differences between mysql, mysqli (which will replace mysql at some point in the future) and PDO methods of interacting with a MySQL database.  The following table which I stumbled upon whilst researching explains it pretty well.

Source: http://www.php.net/manual/en/mysqli.overview.php

The original mysql extension listed in the third column has a development status of maintenance only.  My interpretation of this is that at some point, in a future release this extension will be dropped, meaning it’s time to move on and use something more up to date.

The mysqli (with the i meaning improved) extension looks to be the replacement for the original extension and the preferred for new projects.

The major difference between PDO and Mysqli is (of course) mysqli is limited to a MySQL databases.  PDO is an abstraction layer which can interact with a number of databases – the complete list of drivers are here.

Reading further into the two, another key difference between the two is that PDO allows prepared statements with named parameters.

So where the following would work with both PDO and mysqli

SELECT * FROM table WHERE id = ? AND display = ?

Only the following would work with PDO

SELECT * FROM table WHERE id = :id AND display = :display

Not a great difference, however the second option would in my eyes, be the preferred method of doing a prepared statement for the sake of readability.

Therefore, for my project, I will dump the old mysql extension and handle all database actions through the PDO layer.

My first semester back at University is almost complete, only two assignments (both completed) to hand in and then I’m done!  This semester has been quite easy, I’ve had to do 6 assignments in total, 4 in Java and 2 in .Net.  All the projects have been to create some kind of application, be it for a company who’s employee hierarchy resembles a pyramid scheme but overall nothing too hard.  At the end of it all, I can say I know Java, which opens the way for some Android development when the GooglePhone/Nexus One comes to the UK.

A couple of posts back I said I would learn C++ whilst learning Java.  I can honestly say this never happened, however I don’t see learning the language a challenge anymore.  One day I will finally get to program something in this language but I wont be going out of my way to do something in it.

A new year has forced me to get back into a exercise regime again.  So far I’ve been good and eventually I might be able to report some losses or gains.  My goal at the moment is to try and stay the same weight but lose some fat in exchange for muscle.

Onto work… The new year saw me working from home for the first time thanks to the weather (constant snow since Christmas).  A very different experience compared to working in a office (how do people work from home?) I will not miss it.  We have started a couple of new projects, both based in PHP using SOAP connected to a MySQL database, Good fun!.  Negotiated a pay rise over MSN :) and a bag of holidays left to take.

Thats it for now.

Ok so blogging isn’t really up there with my priorities. This just means I write more less often.

My semester at Uni is nearly over, only a couple of weeks left followed by a short break and i’ll be starting the next semester. The most interesting thing i’ve learned so far this year Object Orientatation. When I started my job at 3DPixel.net my first project was using PHP and OO techniques and I can honestly say I didn’t understand most of it at first but now it all makes sense.

I’ve started on a new personal project (http://rentopoly.com) to pass the time (when I have free time) which is a website targetted at students and landlords looking to rent properties. I will be using my new found knowledge of OO to build the website.

I have so far created one object which I think could be interesting for other people to use. This object connects to Yahoo! MapService API to resolve an address (via Curl) into geo latitude and longitude data. This is usefull if you need addressing lookup without having to purchase data like that supplied by the Post Office (PAF File).

To use the object;

$myAddress = new Address("House and Street Name", "Locality", "City Town", "Postcode");
echo $myAddress->getLatitude();
echo $myAddress->getLongitude();
/* You could also use the object to display address information */
echo $myAddress->getAddress1();
echo $myAddres->getCity();

You can use this with GoogleMaps to place a marker on a map.

Full object source.

/*
 * Creates an address object
 * See - http://eighty-six.co.uk/blog/2009/12/oo-techniques/
 * @author Damon Skelhorn
 */
class Address {

	private $postcode;
	private $address1;
	private $address2;
	private $city;
	private $longitude;
	private $latitude;
	private $url = "http://local.yahooapis.com/MapsService/V1/geocode?appid=yourapikey";

	function __construct($address1, $address2, $city, $postcode){
		$this->setAddress1($address1);
		$this->setAddress2($address2);
		$this->setCity($city);
		$this->setPostcode($postcode);
		$this->resolveAddress();
	}

	private function resolveAddress() {
		$stream = "location=" . $this->getAddress1() . ",".$this->getCity() . "," . $this->getPostcode() . '&output=php';
		$res = $this->fetchPage($this->getUrl(), TRUE, $stream, '');
		$res = unserialize($res);
		$this->setLongitude($res['ResultSet']['Result']['Longitude']);
		$this->setLatitude($res['ResultSet']['Result']['Latitude']);
	}

	private function fetchPage($url, $ispost, $params, $auth){
		if(!$ispost && $params) $url .= '?' . $params; //if not post and params (ie, if it's a get)
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		if(is_array($auth)){
			curl_setopt($ch, CURLOPT_USERPWD, $auth['username'] . ':' . $auth['password']);
			curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		}
		if($ispost){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		}
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}

    public function getAddress1() {
        return $this->address1;
    }

    private function setAddress1($address1) {
        $this->address1 = $address1;
    }

    public function getAddress2() {
        return $this->address2;
    }

    private function setAddress2($address2) {
        $this->address2 = $address2;
    }

    public function getCity() {
        return $this->city;
    }

    private function setCity($city) {
        $this->city = $city;
    }

    public function getPostcode() {
        return $this->postcode;
    }

    private function setPostcode($postcode) {
        $this->postcode = $postcode;
    }

    private function getUrl() {
        return $this->url;
    }

    private function setUrl($url) {
        $this->url = $url;
    }

    public function getLatitude() {
        return $this->latitude;
    }

    private function setLatitude($latitude) {
        $this->latitude = $latitude;
    }

    public function getLongitude() {
        return $this->longitude;
    }

    private function setLongitude($longitude) {
        $this->longitude = $longitude;
    }
}
?>