Showing posts with label php oop. Show all posts
Showing posts with label php oop. Show all posts

Monday, February 4, 2008

Simple PHP Database Connection Class

server  = $_DBINFO['server'];
$this->username = $_DBINFO['username'];
$this->password = $_DBINFO['password'];
$this->database = $_DBINFO['database'];
$this->connect();
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx CREATES A SERVER CONNECTION xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function connect(){
$this->conn = @mysql_connect($this->server, $this->username, $this->password);
if($this->conn == FALSE){
echo 'Cannot connect to server \''.$this->server.'\'.'; exit;
}
$dbconnect = @mysql_select_db($this->database, $this->conn);
if($dbconnect == FALSE){
echo 'Unable to connect to database \''.$this->database.'\''; exit;
}
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx PROCESS SQL QUERY xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function query($sql){
$this->query = @mysql_query($sql, $this->conn) or print_r(mysql_error());
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx QUERY RESULT IN ARRAY FORM xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function getRecord(){
$resultCount = mysql_num_rows($this->query);

if ($resultCount==1){
$result = @mysql_fetch_assoc($this->query);
}
else if ($resultCount > 1){
while($row = @mysql_fetch_assoc($this->query)){
$result[]=$row;
}
}
else{
$result = NULL;
}
return $result;
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx GET TOTAL RESULT COUNT xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function query_count(){
return @mysql_num_rows($this->query);
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx GET LAST INSERTED ID xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function last_insert_id(){
return @mysql_insert_id($this->conn);
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx RETURN ONLY ONE SPECIFIC FIELD xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function getField($fieldname){
while($row = @mysql_fetch_assoc($this->query)){
$result[]=$row[$fieldname];
}
return $result;
}
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx HOW-TO-USE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// A simple demo on how to use this module
// DIRECTION: download file and save then remove the ".txt" extension to be saved as a php file
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
/*
// ASSIGN DATABASE SERVER INFO INTO ARRAY VALUES
$serverInfo['server'] = 'localhost';
$serverInfo['username'] = 'root';
$serverInfo['password'] = '';
$serverInfo['database'] = 'rednixchat';

// CREATE NEW Database OBJECT
$db = new Database($serverInfo)

// CREATE AN SQL STATEMENT
$sqlSelect = "SELECT * FROM user_info";

// PERFORM SQL STATEMENT ASSIGNED IN $sqlSelect
$db->query($sqlSelect);

// GET TOTAL RESULT FOR SQL QUERY
$totalResult = $db->query_count();

// GET QUERY RESULT IN ARRAY FORM
$queryResult = $db->getRecord();

// DISPLAY QUERY RESULT
print_r($queryResult);
*/
?>

Wednesday, October 10, 2007

Simple PHP Class Example Part 1

$item = new Items;
$item -> setName("Book");
$book = $item->getName();

$item -> setName("Pencil");
$Pencil = $item->getName();

class Items
{
var $item;

function setName($newname)
{
$this->title=$newname;
}

function getName()
{
return $this->title;
}
}

Simple PHP Class Example Part 2

/*Create a new Object*/
$pricelist = new PriceList;

/*Set item name and price*/
$pricelist-> setItem("Carrot",10);

/*Get Item information*/
$itemname = $pricelist->getItemName();
$itemprice = $pricelist->getItemPrice();
$iteminfo = $pricelist->getItemInfo();

/*Display Item Information*/
echo "Name: $itemname \n";
echo "Price: $itemprice
\n";
echo "Item Information: $iteminfo
\n";
/*
OUTPUT
Name: Carrot
Price: 10
Item Information: Item=Carrot, Price=10
*/

/* This is the class object */
/* The Class name is "PriceList"*/
class PriceList
{
var $item; // -> this will hold the item name
var $price; // -> this will hold the item price
var $iteminfo; // -> this will hold the item info

/* This function will set the item name and price and store them in "inteminfo"*/
function setItem($newitem, $newprice)
{
$this->item=$newitem;
$this->price=$newprice;
$this->iteminfo = "Item=".$this->item.", Price=".$this->price;
}
/* This function will return the item info of the object */
function getItemInfo()
{
return $this->iteminfo;
}
/* This function will return the item name of the object */
function getItemName()
{
return $this->item;
}
/* This function will return the item price of the obj*/
function getItemPrice()
{
return $this->price;
}
}