PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of ter   PDO ActiveRecord   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example, showed how to use class
Class: PDO ActiveRecord
Store and retrieve objects from databases with PDO
Author: By
Last change: bugfix
Date: 15 years ago
Size: 1,360 bytes
 

Contents

Class file image Download
<?php
include_once("ActiveRecord.php");
  
$db = new PDO("mysql:host=localhost;dbname=test", "root", "");
  
ActiveRecord::setDefaultDBConnection($db);
  
$res = new ActiveRecord("users");

//some random data
$res->fullname = "rainman";
$res->address = "Your hat are too old";
$res->icq = "333-333-333";
$res->jabber = "jabber@jabber.org";
$res->email = "email@email.org";
$res->site = "http://yoursite.org/";
$res->date_reg = date("Y-m-d"); //date (datetime, date, time types) can be anything supported by strtotime
$res->date_visit = time() + 3600; //or just seconds from UNIX epoch
$res->phone = "333-33-33";

$res->commit(); //insert

$res->fullname = "Mr. Toot";
$res->date_reg = "1970-01-01 00:00:00"; //UNIX epoch

$res->commit(); //update

$key = $res->getKey(); //get our row's key

//new instance with key specified
$newRes = new ActiveRecord("users", $key);

//selecting from database
echo "My name is {$newRes->fullname}";


//subclassing also supported
class Person extends ActiveRecord {

    public function
__construct($key = null, $database = null){
       
parent::__construct("users", $key, $database);
    }
}

$person = new Person(1);

$person->fullname = "Mr. Dush";
echo
"Ahh my new name is {$person->fullname}, greetz";

$person->commit(); //update, only 'fullname' field will updated