NameSilo

Useful Functions and Classes

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
Watch

wackyjoe

Established Member
Impact
3
Hello There,

Here i will be posting any useful functions or classes i have made or come across.

Would be nice if someone pinned this as it would come in handy.

Please feel Free to add your own functions/classes.

thank you,
Russell Dias

Useful Functions
PHP:
// Checks if email is valid

function is_valid_email($email)
{
	return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $email);
}

//Converts A-Z to a-z

function startolower($string)
{
	global $stylevar;
	if (function_exists('mb_strtolower') AND $newstring = @mb_strtolower($string, $stylevar['charset']))
	{
		return $newstring;
	}
	else
	{
		return strtr($string,
			'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
			'abcdefghijklmnopqrstuvwxyz'
		);
	}
}

//random number generator

function vbrand($min, $max, $seed = -1)
{
	if (!defined('RAND_SEEDED'))
	{
		if ($seed == -1)
		{
			$seed = (double) microtime() * 1000000;
		}

		mt_srand($seed);
		define('RAND_SEEDED', true);
	}

	return mt_rand($min, $max);
}

//fetches file extension

function file_extension($filename)
{
	return substr(strrchr($filename, '.'), 1);
}


Useful Classes

MYSQL Class

PHP:
<?
require_once("constants.php");
require_once("functions.php"); 
require_once("func.security.php"); 


class MYSQL
{
	var $linkid = 0;
	var $qid = 0;
	var $dbid = 0;
	var $qtime = 0;
	var $qcount = 0;


	function MYSQL($host='localhost', $dbname='DBNAME', $user='USERNAME', $pass='PASSWORD', $persist=false) 
	{
	
		if( $user AND $host )
			$this->connect($host, $user, $pass, $persist);
		if( $dbname AND $this->linkid )
			$this->select_db($dbname); 
	}
	
	function connect($host, $user='USERNAME', $pass='PASSWORD', $persist=false)
	{
		if( $persist )
			$this->linkid = mysql_pconnect($host, $user, $pass) or die(mysql_error());
		else
			$this->linkid = mysql_connect($host, $user, $pass, true) or die(mysql_error());
	}
	
	function select_db($dbname)
	{
		$this->dbid = mysql_select_db($dbname, $this->linkid) or die(mysql_error());
	}
	
	function query($sql, $setid=1)
	{
		$qstart = explode(' ', microtime());
		
		$qid = mysql_query($sql, $this->linkid) or die(mysql_errno($this->linkid) .' - '. mysql_error($this->linkid) .'<br /> SQL:<br />'. html($sql));
		
		$qend = explode(' ', microtime());
		$this->qtime += ($qend[0]+$qend[1])-($qstart[0]+$qstart[1]);
		$this->qcount++;
		if( $setid )
			$this->qid = $qid;
		return $qid;
	}
	
	function fetch_array($qid=false, $type=MYSQL_ASSOC)
	{
		if( !$qid )
			$qid = $this->qid;
		return mysql_fetch_array($qid, $type);
	}
	
	function fetch_object($qid=false, $type=MYSQL_ASSOC)
	{
		if( !$qid )
			$qid = $this->qid;
		return mysql_fetch_object($qid);
	}
	
	function first_array($sql, $type=MYSQL_ASSOC)
	{
		$qid = $this->query($sql, 0);
		$f = $this->fetch_array($qid, $type);
		$this->free_result($qid);
		return $f;
	}
	
	function first_object($sql, $type=MYSQL_ASSOC)
	{
		$qid = $this->query($sql, 0);
		$f = $this->fetch_object($qid, $type);
		$this->free_result($qid);
		return $f;
	}
	
	function free_result($qid=false)
	{
		if( !$qid )
			$qid = $this->qid;
		@mysql_free_result($qid);
	}
	
	function result($col=0, $row=0, $qid=false)
	{
		if( !$qid )
			$qid = $this->qid;
		return @mysql_result($qid, $row, $col);
	}
	
	function result_sql($sql, $col=0)
	{
		$qid = $this->query($sql, 0);
		$r = $this->result($col, $row=0, $qid);
		$this->free_result($qid);
		return $r;
	}
	
	function rowcount($sql, $col='count')
	{
		$c = $this->result_sql($sql, $col);
		if( !$c )
			$c = 0;
		return $c;
	}
	
	function num_rows($qid=false)
	{
		if(!$qid)
			$qid = $this->qid;
		return intval(@mysql_num_rows($qid));
	
	}
	
	function insert_id()
	{
		return mysql_insert_id($this->linkid);
	}
	
	function clean($str)
	{
		if( PHP_VERSION >= 5 AND $this->linkid )
			return mysql_real_escape_string($str, $this->linkid);
		return mysql_escape_string($str);
	}
	
	function close()
	{
		@mysql_close($this->linkid);
	}
	
}
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Thanks :) The MySQL class will be usefull! I didnt think it was possible to mysql_fetch_array through a class though?
 
0
Close
wackyjoe said:
Useful Functions
PHP:
//Converts A-Z to a-z

function startolower($string)
{
	global $stylevar;
	if (function_exists('mb_strtolower') AND $newstring = @mb_strtolower($string, $stylevar['charset']))
	{
		return $newstring;
	}
	else
	{
		return strtr($string,
			'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
			'abcdefghijklmnopqrstuvwxyz'
		);
	}
}

There's a built in PHP function to convert a string to lowercase. strtolower()
 
0
Close
wackyjoe said:
PHP:
    function clean($str)
    {
        if( PHP_VERSION >= 5 AND $this->linkid )
            return mysql_real_escape_string($str, $this->linkid);
        return mysql_escape_string($str);
    }
Why PHP 5 ? mysql_real_escape_string is available in PHP >= 4.3.0:
PHP:
    function clean($str)
    {
        if( PHP_VERSION >= '4.3.0' AND $this->linkid )
            return mysql_real_escape_string($str, $this->linkid);
        return mysql_escape_string($str);
    }
or
PHP:
    function clean($str)
    {
        if(function_exists('mysql_real_escape_string') AND $this->linkid )
            return mysql_real_escape_string($str, $this->linkid);
        return mysql_escape_string($str);
    }
 
0
Close
SecondVersion said:
Why PHP 5 ? mysql_real_escape_string is available in PHP >= 4.3.0:
PHP:
    function clean($str)
    {
        if( PHP_VERSION >= '4.3.0' AND $this->linkid )
            return mysql_real_escape_string($str, $this->linkid);
        return mysql_escape_string($str);
    }
or
PHP:
    function clean($str)
    {
        if(function_exists('mysql_real_escape_string') AND $this->linkid )
            return mysql_real_escape_string($str, $this->linkid);
        return mysql_escape_string($str);
    }


ok thanks for that i couldnt use mysql_real_escape_string on my old php version must have been older then i thought lol, but it works on php 5

CreedFeed said:
There's a built in PHP function to convert a string to lowercase. strtolower()

yes i kno but i had used that in my class, i dont think i should have added that here without the class.

But it was just a layout class and there are alot of them on this site, so i decided against it and just took some of the handy functions
 
0
Close
CatchedCatched

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Search: choose where to search from the All tab
Open the search at the top of any page. On the All tab, the list next to the search box lets you choose where to search: everything, all...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back