Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Model
- {
- public $connection;
- private $_select;
- private $_from;
- private $_where;
- private $_limit;
- private $_orderBy;
- private $_insertInto;
- function __construct()
- {
- $xml = simplexml_load_file("./app_code/config.xml");
- $children = $xml->children();
- $dbHost = $children->database["host"];
- $dbUser = $children->database["user"];
- $dbPass = $children->database["password"];
- $dbName = $children->database["name"];
- $this->connection = mysql_connect($dbHost, $dbUser, $dbPass) or die("Unable to connect to mysql database <br />". mysql_error());
- mysql_select_db($dbName, $this->connection);
- }
- public function select($select)
- {
- $this->_select = "SELECT ".$select;
- return $this;
- }
- public function from($from)
- {
- $this->_from = "FROM ".$from;
- return $this;
- }
- public function where($where)
- {
- $this->_where = "WHERE ".$where;
- return $this;
- }
- public function limit($offset, $length = 0)
- {
- $this->_limit = "LIMIT ".$offset.", ".$length;
- return $this;
- }
- public function orderBy($by, $direction = "DESC")
- {
- $this->_orderBy = "ORDER BY ".$by." ".$direction;
- return $this;
- }
- public function InsertInto($tableName, $data)
- {
- $columnsQuery = mysql_query("SHOW FIELDS FROM ".$tableName);
- $columnsResult = array ();
- while($field = mysql_fetch_assoc($columnsQuery))
- {
- $columnsResult[$field["Field"]] = "";
- }
- $diff = array_diff($columnsResult, $data);
- if(count($diff) > 0)
- {
- die("The fields supplied must match the fields in the table");
- }
- $this->_insertInto = "INSERT INTO `".$tableName."` (";
- $i = 1;
- foreach($data as $key => $val)
- {
- if($i != count($data))
- {
- $this->_insertInto .= "`".$key."`, ";
- }
- else
- {
- $this->_insertInto .= "`".$key."`";
- }
- $i++;
- }
- $this->_insertInto .= ") VALUES (";
- $i = 1;
- foreach($data as $key => $val)
- {
- if($i != count($data))
- {
- $this->_insertInto .= "'".$val."', ";
- }
- else
- {
- $this->_insertInto .= "'".$val."'";
- }
- $i++;
- }
- $this->_insertInto .= ")";
- $result = mysql_query($this->_insertInto);
- $this->_insertInto = "";
- return $result;
- }
- public function GetResult()
- {
- $query = "".$this->_select." ".$this->_from." ".$this->_where." ".$this->_limit." ".$this->_orderBy."";
- $result = mysql_query($query);
- $return = array();
- while($row = mysql_fetch_assoc($result))
- {
- $return[] = $row;
- }
- $this->_select = "";
- $this->_from = "";
- $this->_where = "";
- $this->_limit = "";
- $this->_orderBy = "";
- return $return;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement