Login   Register  
PHP Classes
elePHPant
Icontem

File: EXAMPLE-CLASS.class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Hensel Hartmann  >  Fast DB Class Generator  >  EXAMPLE-CLASS.class.php  >  Download  
File: EXAMPLE-CLASS.class.php
Role: Sample output
Content type: text/plain
Description: Example Output created by the main class
Class: Fast DB Class Generator
Generate classes to access MySQL database tables
Author: By
Last change:
Date: 2 years ago
Size: 5,107 bytes
 

Contents

Class file image Download
<?php
/**
 * Class : Pages
 * Author : Hensel Hartmann
 * Date : January 26, 2012, 8:51 am
 * Description : 
 * 
 * This class was auto-generated with fastDB.classgenerator 
 * (Hensel Hartmann, simpeligent.ch)
 */


class Pages 
{

	private $_id=null;
	private $_name=null;
	public $db=null;
	private $result;
	public $_data=array();
	public $_item=array();

	/**
	 * Public constructor 
	 * establishes a DB connection based on a helper class
	 * usage: 
	 * $p = new Pages(); 
	 * $p->db = connectDB::getConn(); 
	 *
	 */
	public function Pages() 
	{} 


	/**
	 * Public Get one Object by key with no condition 
	 * additional filtering with conditions
	 * ex: $condition = ' status=1 '
	 * set debug to TRUE if you want to see SQL
	 *
	 * @param int $id
	 * @param string $condition
	 * @param boolean $debug
	 * @return array
	 */
	public function getObject($id,$condition=NULL,$debug=NULL) 
	{
		$this->_item=array();
		$SQL = "SELECT * FROM `pages` WHERE `id` = '".mysql_real_escape_string($id)."' ";
		if($condition!=NULL)
		{
			$SQL .= " AND `$condition` ";
		}
		$SQL .= "  LIMIT 1 ";
		if($debug!=NULL)
		{
			 echo " <!-- Debug SQL: ".$SQL." -->\n ";
		}
		$result = mysql_query($SQL,$this->db) or $this->fatal_error("MySQL Query Error: $SQL");

		if ($row = mysql_fetch_array($result))
		{
			$this->_item['id'] = $row['id'];
			$this->_item['name'] = $row['name'];
		}
		return $this->_item;
	}

	/**
	 * Public Get Full Object List with no condition
	 * get filtered Object List with conditions
	 * ex: $condition = ' id=3 AND (foo=bar OR bar<=foo)'
	 * set debug to true if you want to see SQL
	 *
	 * Also supports ORDER BY AND LIMIT
	 *
	 * @param string $condition
	 * @param string $order_by_and_or_limit
	 * @param boolean $debug
	 * @return array
	 */
	public function getList($condition=NULL,$order_by_and_or_limit=NULL,$debug=NULL) 
	{
		$SQL = "SELECT * FROM `pages` ";
		if($condition!=NULL)
		{
			$SQL .= " WHERE $condition ";
		}
		if($order_by_and_or_limit!=NULL)
		{
			$SQL .= $order_by_and_or_limit ;
		}
		if($debug!=NULL)
		{
			 echo " <!-- Debug SQL: ".$SQL." -->\n ";
		}
		$this->result = mysql_query($SQL,$this->db) or $this->fatal_error("MySQL Query Error: $SQL");

		$this->_data = array();
		for ($i = 0, $d = @mysql_num_rows($this->result); $i < $d; $i++)
		{
			$this->_data[$i] = mysql_fetch_assoc($this->result);
		}
		return $this->_data;
	}

	/**
	 * Public method to save an object to DB 
	 * works only if getObject() was called in advance
	 *
	 * @param boolean $debug
	 * @return boolean
	 */
	public function _updateDB($debug=NULL) 
	{
		if($this->_item['id']!='')
		{
			$SQL = "UPDATE `pages` SET ";
					$SQL .= "`name`='".mysql_real_escape_string($this->_item['name'])."'";
 			$SQL .= " WHERE `id`='".mysql_real_escape_string($this->_item['id'])."' LIMIT 1";
			if($debug!=NULL)
			{
				 echo " <!-- Debug SQL: ".$SQL." -->\n ";
			}
			$result = mysql_query($SQL,$this->db);
			if ($result === false)
				return(false);
			else
				return(true);
		}
		else
		{
			return(false);
		}
	}

	/**
	 * Public method to save a new object to DB 
	 *
	 * @param boolean $debug
	 * @return boolean
	 */
	public function createNewObject($debug=NULL) 
	{
		$SQL = "INSERT INTO `pages`  ";
		$SQL .= " ( id,name ) ";
		$SQL .= " VALUES ( 
				''
				,'".mysql_real_escape_string($this->_item['name'])."'
 				) ";
		if($debug!=NULL)
		{
			 echo " <!-- Debug SQL: ".$SQL." -->\n ";
		}
		$result = mysql_query($SQL,$this->db);
		if ($result === false)
			return(false);
		else
			return(true);
	}

	/**
	 * Public method to delete an object from DB 
	 *
	 * @param int $key_val
	 * @param boolean $debug
	 * @return boolean
	 */
	public function deleteObject($key_val,$debug=NULL) 
	{
		if($key_val!='')
		{
			$SQL = "DELETE FROM `pages`  ";
			$SQL .= "  WHERE `id`='".mysql_real_escape_string($key_val)."' ";
			if($debug!=NULL)
			{
				 echo " <!-- Debug SQL: ".$SQL." -->\n ";
			}
			$result = mysql_query($SQL,$this->db);
			if ($result === false)
				return(false);
			else
				return(true);
		}
	}

	/**
	 * Private method Fatal Error 
	 *
	 * @return string
	 */
	public function fatal_error($error_msg="") 
	{
		$the_error .= "\n\n\nSQL error: ".mysql_error()."\n";
		$the_error .= "SQL error code: ".mysql_errno()."\n";
		$the_error .= "Date: ".date("l dS of F Y h:i:s A");
		$out = '<html><head><title>FastDB-Generator MySQL Error</title></head><body>
                        &nbsp;<br /><br /><blockquote><b>There has been an error with your database.</b><br />
                        <br /><br /><b>Error Returned</b><br />
                        <form name="mysql"><textarea rows="15" cols="60">".htmlspecialchars($error_msg)."".$the_error."</textarea></form><br />
                        We apologise for any inconvenience<br />fastDB-Generator by http://simpeligent.ch</blockquote></body></html>';
		print $out;
		exit();
	}

}
?>