PHP Sql Wrapper Class

A simple class to handle database operations when you don't have PDO. It uses Singleton to handle an unique database connection.

/**
 * Sql class that handles database operations without PDO
 *
 * Sample use:
 * ---
 * Select One record:
 *   $sql="SELECT * FROM user WHERE email=".SqlWrapper::quote_smart($email)." ";
 *   $userData=SqlWrapper::selectOne($sql);
 *
 * Select All records
 *   $sql="SELECT * FROM user ";
 *   $userData=SqlWrapper::selectAll($sql);
 *
 * Execute query
 *   $sql="DELETE FROM user WHERE id=0";
 *   SqlWrapper::executeCommand($sql);
 *
 * @author freedelta http://freedelta.free.fr
 * @version2.0: : Sql date 2011-02-09 $
 * @access public
 */


class SqlWrapper
{
   /* Database handler */
    private static $dbh=null;
    
    /* Get only one instance */
    static public function getDb()
    {
        if(self::$dbh==null)
        {
            $server="";
            $dbname="";
            $user="";
            $password="";
            
            if( self::$dbh = mysql_connect ($server, $user, $password) )
            {
                mysql_select_db ($dbname,self::$dbh);
            }  
            else
            {
                echo 'Error:' . mysql_error();
            }
        }
        return self::$dbh;
    }
    
    /**
     * Retrieves a collection for a given query
     *
     * @param String sql query
     * @return Collection data
     */
   static public function selectAll ( $sql )
    {   
        $rows=array();
        
        $rset = mysql_query($sql, self::getDb()) or die($sql.mysql_error());
        if($rset)
        {
            while ($row = mysql_fetch_assoc($rset)) { $rows[]= $row; }   // Loop through the results
        }
        return $rows;
    }    
    
    /**
     * Retrieves a collection for a given query
     *
     * @param String sql query
     * @return Collection data
     */
   static public function selectOne ( $sql )
    {
        $rows=array();
        
        $rset = mysql_query($sql, self::getDb()) or die($sql.mysql_error());
        
        if($rset)
        {
            while ($row = mysql_fetch_assoc($rset)) { $rows[]= $row; }   // Loop through the results
            
        }
        else
        {
            $rows[0]=null;
        }
        
        return $rows[0];
        
    }
    
    /**
     * Execute standard queries
     *
     * @param string Sql query
     * @return number of affected rows
     */
    static public function executeCommand ( $sql )
    {
        $query_id = mysql_query($sql, self::getDb()) or die($sql.mysql_error());

        if (!$query_id)
        {
            echo ("MySQL Query fail: $sql");
            return 0;
        }
        
        $affected_rows = mysql_affected_rows(self::getDb());

        return $affected_rows;
        
    }
    
    /**
     * Get last insert id for current conection
     */
    static public function getLastInsertId()
    {
       return mysql_insert_id();
    }
    
    /**
     * Protection from sql injections
     *
     */    
    public static function quote_smart($value)
    {
       if (get_magic_quotes_gpc())
       {
         $value = stripslashes($value);
       }
      
       $value = "'" . mysql_real_escape_string($value,self::getDb()) . "'";

       return $value;
    }
    
    static public function closeDb()
    {
        if(mysql_ping(self::getDb()))
        {
            mysql_close(self::getDb());
        }
    }
    
    
    function __destruct()
    {
        $this->dbh=null;
    }
}
?>