php & mysql

Coding a simple Guestbook using PHP and MySQL

his is a short article which shows you how to make a simple guestbook by capturing the visitor’s input and storing the data in a database using PHP. You’ll need to know a bit of HTML, PHP and MySQL in order to do the task. For a guestbook to work, we’ll need to create 3 pages.

  1. We’ll need a form to prompt the visitor to leave a comment in your guestbook. Let’s call this page comments.php .
  2. We’ll need to create some PHP and MySQL code to capture the visitor’s input and save it to the database. Let’s call this page store_comment.php.
  3. Finally, More >

Classes and OOP in PHP

Object orientated programming (OOP) is a commonly used term when discussing modern programming techniques. One of the things that makes humans stand out is the ability to categorise – we put objects into categories of similar type of function. For example, we have the category vechicle. Within this category, you have the different types of vehicle – car, bug, train, van. In the same bag, a llama is a type of animal. This analogy applies to programming – we create functions to do specific tasks and then group similar functions into classes. No only does this organise everything, but also More >

5-second RSS parser w/ simpleXML

During Rasmus’s talk last week at the php|works conference, he showed how easy it is to parse XML in PHP 5, using simpleXML. In his example, he used the Flickr public photo RSS feed, to parse and output. A lot of people don’t know about this feed, which gets updated the moment a new photo gets uploaded. Furthermore, a lot of people upload their personal photos, and make them private after their uploaded. Click here to see it in action. Here are the 2 lines of code:

Code:
<?php
    $url = 'http://www.flickr.com/services/feeds/photos_public.gne';
    foreach(simplexml_load_file($url)->entry as $it) echo $it->content;
?>

Be sure to mark you More >

How to: Building dynamic PHP objects and URL decoding example example

PHP is a powerful programming language; with every release I’m amazed with the new power and functionality. The ability to build dynamic objects (the way I’ll describe) I believe was introduced as of PHP 5. In any case, this is good stuff. There are lots of reasons why you would want to create a dynamic object, in my example below; you’ll see how to create a dynamic object of the URL query parameters.

At the heart of building dynamic PHP objects are the PHP Magic Methods. From the PHP manual:

The function names __construct, __destruct (see Constructors and Destructors), __call, __get, __set, More >

How To: Making a PHP REST client to call REST resources

A lot of companies these days (including Amazon and Yahoo!) are exposing their web services in the form of REST resources. At a high level REST is pretty easy to understand, all you’re doing is exposing a web service in the form of a URL. Users can then query this URL, through HTTP methods like GET and POST. REST calls generally return some type of XML or Object Encoding like JSON.

An example would be Yahoo!’s Geocoding API, with the following URL:

Code:
http://api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&street=701+First+Street&city=Sunnyvale&state=CA

I would get:

Code:
<Result precision="address">
   <Latitude>37.416384</Latitude>
   <Longitude>-122.024853</Longitude>
   <Address>701 FIRST AVE</Address>
   <City>SUNNYVALE</City>
   <State>CA</State>
   <Zip>94089-1019</Zip>
   <Country>US</Country>
</Result>

So Yahoo! exposes the Geocode URL More >

A closer look into what Google Adwords charges..

This is interesting, though on a small scale, it’s still relevant. I run Google Adwords (display ads on specific search terms with Google) for a service I created called WebBasedCron. A little while ago, I decided to start tracking what ads were really being clicked — i.e. when an advertisement was clicked, it would send them to a special URL (ex. www.webbasedcron.com/?action=advwbc). Then I would use PHP code like the following to record the persons IP, the date, time and their session id.

Code:
if ( $read_action == "advwbc" ) {
        $session = $user->db->quote(session_id());
        $ip = $user->db->quote($_SERVER['REMOTE_ADDR']);
        $date = $user->db->quote(date("'Y-m-d'"));
        $time More >

PHP: Double quotes vs Single quotes

If you don’t know the difference between using double quotes or single quotes when using Strings in PHP, you should. The PHP Manual page for Strings covers all this, but basically strings created with double quotes will be parsed by PHP. This example shows what I mean:

Code:
// they both output 'Yahoo Google'
$str = 'Google';
echo 'Yahoo' . $str;
echo "Yahoo $str";

At first glance you might think, who cares? The key note is deep in the manual page:

Parsing variables within strings uses more memory than string concatenation. When writing a PHP script in which memory usage is a concern, consider using More >

PHP: Including scripts from within class functions

This surprised me today: Using PHP you can include scripts from within a class functions and access the included functions globally. It seems to go against what I feel should occur, but it works perfectly fine. I posted on a discussion forum at devshed to see if anyone has some insight into this.

temp_include.php:

Code:

<?php

function test_funct() {
    echo "function called";
}

?>

Main script:

Code:

<?php

Class ABC {
    public function __construct() {
        require_once 'temp_include.php';
    }
}

$o = new ABC();
test_funct();

?>

PHP: Creating a singleton class for serialization

There’s a lot of examples on the internet about creating and using the singleton pattern within your application. From the PHP Docs:

The Singleton pattern applies to situations in which there needs to be a single instance of a class. The most common example of this is a database connection. Implementing this pattern allows a programmer to make this single instance easily accessible by many other objects.

Within any class you want to have use the singleton pattern, just add a getInstance method, like I have in the Obj code listing below. To access that instance use the scope resolution operator:

Code:
$obj = More >

Book: Learning PHP Data Objects

I don’t normally read programming books, but I was given a copy of Learning PHP Data Objects as a gift a couple moths ago and thought I’d give it a spin. Though the book is a beginners guide to PDO, it requires the reader to have a working knowledge of PHP and Data Abstraction. I was particularly curious to read more about PDO, since I had always used the PEAR database abstraction classes (now MDB2), and wanted to see what, if anything, I was missing out on. I know what your thinking: PDO is compiled into PHP and the PEAR classes More >

Get Adobe Flash playerPlugin by wpburn.com wordpress themes