TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-07-2008, 08:57 PM   #1 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default 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.
TlcAndres is offline  
Reply With Quote
The Following User Says Thank You to TlcAndres For This Useful Post:
Alan @ CIT (03-08-2008)
Old 03-08-2008, 01:38 PM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Thanks mate, very handy - bookmarked

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:37 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design