Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- define('__HOST__',"localhost");
- define('__USERNAME__',"root");
- define('__PASSWORD__',"");
- define('__DATABASE__',"sampledatabase");
- // the sql singelton object of _mysql class
- function sql($query=null,$return_result = false) {
- global $_mysql;
- if (!isset($_mysql)) {
- $_mysql = new _mysql( __HOST__, __USERNAME__, __PASSWORD__ , __DATABASE__ );
- }
- if ($query!==null) {
- return $_mysql->execute($query,$return_result);
- }
- return $_mysql;
- }
- // sample 1 --> insert
- /*
- $row = array("id"=>"1", "name" => "John" , "age" => "30");
- $result_id = sql()->insert("persons",$row);
- if ( $result_id != null ) {
- echo "inserted new row!";
- } else {
- echo mysql_error();
- }
- */
- // sample 2 --> update
- /*
- $row = array("name" => "John" , "age" => "31");
- $affected_rows = sql()->update("persons",$row," id = 1 ");
- if ($affected_rows) {
- echo "$affected_rows row(s) updated!";
- } else {
- if ($err = mysql_error()) {
- echo $err;
- } else {
- echo "No rows affected!";
- }
- }
- */
- // THE MYSQL WRAPPER CLASS
- class _mysql {
- private $link;
- private $result;
- // BASIC
- function __construct($host,$username=null,$password=null,$database=null) {
- $this->connect($host,$username=null,$password=null,$database=null);
- return $this;
- }
- function __destruct() {
- $this->close();
- }
- function connect($host,$username=null,$password=null,$database=null) {
- if (is_array($host)) extract($host);
- $this->link = mysql_connect($host,$username,$password);
- mysql_select_db($database);
- return $this->result;
- }
- function close() {
- @mysql_close($this->link);
- }
- function execute($q,$return_result=false) {
- $this->result = mysql_query($q);
- return ($return_result) ? $this->result : $this;
- }
- // DATA ADJUSTMENTS
- public function secure($data) {
- if (is_array($data)) {
- foreach ($data as $i=>$d) {
- $data[$i] = mysql_real_escape_string($d);
- }
- } else {
- $data = mysql_real_escape_string($data);
- }
- return $data;
- }
- // QUERY BUILDING
- public function generate_fields($data) { // note: All values get secured
- $fields = $values = array();
- foreach ($data as $key=>$value) {
- $fields[]="`".$key."`";
- $values[]="\"".$this->secure($value)."\"";
- }
- return array( $fields, $values );
- }
- // COMMON QUERIES
- public function insert($table,$data) { // inserts a single row into table
- list($fields,$values) = $this->generate_fields($data);
- $fields="(".implode(",",$fields).")";
- $values="(".implode(",",$values).")";
- $q = "insert into `$table` {$fields} values {$values};";
- $this->execute($q);
- return mysql_insert_id();
- }
- public function update($table,$data,$where,$limit='') {
- if ($where) $where="where $where";
- if ($limit) $limit="limit $limit";
- list( $fields, $values ) = $this->generate_fields($data);
- foreach ($fields as $key=>$field) {
- $updates[]="{$field}={$values[$key]}";
- }
- $updates=implode(",",$updates);
- $q="update `$table` set {$updates} $where $limit;";
- $this->execute($q);
- return mysql_affected_rows();
- }
- public function delete($table,$where='',$limit='') {
- if ($where) $where="where $where";
- if ($limit) $limit="limit $limit";
- $this->execute("delete from `$table` $where $limit;");
- return mysql_affected_rows();
- }
- }
- ?>
Add Comment
Please, Sign In to add comment