03-07-2008, 08:57 PM
|
#1 (permalink)
|
|
The Addict
Join Date: Nov 2007
Posts: 264
Thanks: 2
|
Some functions and a class I use alot
Well I use a DB class repeatedly for almost every project I work with and It's pretty short and does it's job
DB class
Essentially
PHP Code:
<?php
/** * Handles the database */
class mrf_db { /** * Holds the query resource ID */ private $queryId; /** * Holds the connection ID */ private $connectionId; /** * Keeps connection variables */ private $conVars = array(); /** * Keeps count of all queries */ public $queryCount; /** * Keeps track of the DB */ public $db;
public function __construct($host,$user,$pass,$db) { $this->conVar = array($host,$user,$pass,$db); $this->SetDB(); } public function SetDB($db='') { $db = empty($db) ? $this->conVar[3] : $db; if($this->connectionId) {mysql_close($this->connectionId); } $this->connectionId = mysql_connect($this->conVar[0],$this->conVar[1],$this->conVar[2]); mysql_select_db($db,$this->connectionId); } public function Execute($sql) { $this->query($sql); return new Record($this->queryId); } public function GetOne($sql) { $this->query($sql); return @reset(@mysql_fetch_assoc($this->queryId)); } public function GetRow($sql) { $this->query($sql); return @mysql_fetch_assoc($this->queryId); } public function GetCol($sql) { $this->query($sql); while($row = mysql_fetch_assoc($this->queryId)) { $col[++$i] = reset($row); } } public function sprintfe() { $sql = func_get_args(); $escape = array_map('mysql_real_escape_string',array_splice($sql,1)); return call_user_func_array('sprintf',array_merge($sql,$escape)); }
private function query($sql) { $this->queryCount++; $this->queryId = mysql_query($sql,$this->connectionId); } }
class Record { private $queryId; public $fetch; public $fields; public $EOF = false; public $count; public function __construct($id) { $this->fetch = MYSQL_ASSOC; $this->queryId = $id; $this->fields = @mysql_fetch_array($id,$this->fetch); } public function MoveNext() { if($this->fields = @mysql_fetch_array($this->queryId,$this->fetch)) { $this->count++; return true; } if(!$this->EOF) { $this->EOF = true; return true; } } }
?>
Examples:
PHP Code:
$db = new mrf_db('localhost','root','','wordpress'); $d = $db->Execute('SELECT * FROM `wp_users`'); echo $d->fields['username'];
PHP Code:
$db = new mrf_db('localhost','root','','wordpress'); //same as $d = $db->GetOne('SELECT * FROM `wp_users`'); $d = $db->GetOne('SELECT `id` FROM `wp_users`'); echo $d;
Some functions I use all the time
PHP Code:
function emailvalidate($email) { if(function_exists('filter_var')) { return (bool)filter_var($email,FILTER_VALIDATE_EMAIL); }elseif(function_exists('checkdnsrr')) { $host = next(explode('@',$email)); if(!empty($host) && checkdnsrr($host,'MX')) { return true; } } return (bool)preg_match('/^([a-z0-9._-](\+[a-z0-9])*)+@[a-z0-9.-]+\.[a-z]{2,6}$/i',$email); }
function GenVal($len=20) { $ret = ''; for($i=0;$i != $len;++$i) { $ret .= chr(0x3 . rand(1,80)); } return $ret; }
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
|
|
|
|