- 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
Useful Classes
MYSQL Class
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);
}
}








