<?php
if (!defined('ANTIHACK'))
{
header('HTTP/1.0 403 Forbidden');
header('Status: 403 Forbidden');
die ("<br> You don't have access here.");
}
Class MySQL
{
var $dbConfigInfo;
var $connect;
var $row;
public static function connect($dbConfigInfo = NULL)
{
static $connect;
if (!is_array($dbConfigInfo) || count($dbConfigInfo) < 1)
return FALSE;
if (! isset ($connect))
{
$connect = mysql_connect($dbConfigInfo['servername'], $dbConfigInfo['username'], $dbConfigInfo['password']);
return $connect;
}
}
public static function db($dbname)
{
$db = mysql_select_db($dbname) or die(mysql_error());
return $db;
}
public static function query($query)
{
return mysql_query($query, $connect);
}
public static function FetchArray($result = '')
{
return $row = mysql_fetch_array($result, MYSQL_BOTH);
}
}
?>
and this is how i use it
<?php
$dbConfigInfo = array(
'servername' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'gallery',
);
define('TBL_PRODUCTS', 'sometable');
define('TBL_CATEGORYS', 'sometable');
DEBUG ? error_reporting(E_ALL | E_STRICT) : error_reporting(0);
if(!class_exists('MySQL'))
include_once('classes/mysql.class.php');
$connect = MySQL::connect($dbConfigInfo);
if(!$connect)
die("Nu m-am putut conecta"); // TODO : Modificare erroare handler errori custom
$db_select = MySQL::db($dbConfigInfo['dbname']);
if(!$db_select)
die("Nu am ptutu selecta."); // TODO : Modificare erroare handler errori custom
?>
Refactorings
No refactoring yet !
Tim Cooper
April 3, 2009, April 03, 2009 19:26, permalink
Just a note -- PHP already has a great database wrapper which I highly recommend you use: http://php.net/PDO
Byron
April 3, 2009, April 03, 2009 21:01, permalink
Hello. That's very nice site but I've seen this before here http://text.inguaro.com/e3a8467a8005187b01a737aba0830b81
e3a8467a8005187b01a737aba0830b81
Hello. That's very nice site but I've seen this before here http://text.inguaro.com/e3a8467a8005187b01a737aba0830b81 e3a8467a8005187b01a737aba0830b81
fantomel
April 4, 2009, April 04, 2009 08:37, permalink
i know about PDO and mysqli and i now that some of you consider writing a class for mysql it's a waste of time when instead of using mysql i can be used mysqli or PDO.
The scope of the class is for learning purpose:)
Alix Axel
June 16, 2009, June 16, 2009 00:58, permalink
my 0.02€
<?php
class MySQL
{
public $db = null;
function __construct($database, $username, $password, $hostname = 'localhost', $port = 3306)
{
$this->db = mysql_connect($hostname . ':' . intval($port), $username, $password);
if (is_resource($this->db) === true)
{
if (mysql_select_db($database, $this->db) === true)
{
$this->Query('SET NAMES ' . $this->Quote('utf8') . ';');
$this->Query('SET CHARACTER SET ' . $this->Quote('utf8') . ';');
}
}
}
function __destruct()
{
if (is_resource($this->db) === true)
{
mysql_close($this->db);
}
}
function Query($sql, $type = 'assoc')
{
if (is_resource($this->db) === true)
{
$query = mysql_query($sql, $this->db);
if (is_resource($query) === true)
{
$result = array();
for ($i = 0; $i < mysql_num_rows($query); $i++)
{
$result[$i] = call_user_func('mysql_fetch_' . $type, $query);
}
mysql_free_result($query);
return $result;
}
else if ($query === true)
{
switch (strtoupper(substr($sql, 0, strpos($sql, ' '))))
{
case 'INSERT':
return mysql_insert_id($this->db);
break;
case 'UPDATE':
case 'DELETE':
return mysql_affected_rows($this->db);
break;
}
return true;
}
}
return false;
}
function Quote($string)
{
if (get_magic_quotes_gpc() === true)
{
$string = stripslashes($string);
}
return '\'' . mysql_real_escape_string(trim($string), $this->db) . '\'';
}
}
$db = new MySQL('test', 'root', '');
var_dump($db->Query('INSERT ...'));
var_dump($db->Query('DELETE ...'));
var_dump($db->Query('UPDATE ...'));
echo '<pre>';
print_r($db->Query('SELECT ...'));
echo '</pre>';
?>
murugan
September 23, 2009, September 23, 2009 06:29, permalink
test
<?php
class MySQL
{
public $db = null;
function __construct($database, $username, $password, $hostname = 'localhost', $port = 3306)
{
$this->db = mysql_connect($hostname . ':' . intval($port), $username, $password);
if (is_resource($this->db) === true)
{
if (mysql_select_db($database, $this->db) === true)
{
$this->Query('SET NAMES ' . $this->Quote('utf8') . ';');
$this->Query('SET CHARACTER SET ' . $this->Quote('utf8') . ';');
}
}
}
function __destruct()
{
if (is_resource($this->db) === true)
{
mysql_close($this->db);
}
}
function Query($sql, $type = 'assoc')
{
if (is_resource($this->db) === true)
{
$query = mysql_query($sql, $this->db);
if (is_resource($query) === true)
{
$result = array();
for ($i = 0; $i < mysql_num_rows($query); $i++)
{
$result[$i] = call_user_func('mysql_fetch_' . $type, $query);
}
mysql_free_result($query);
return $result;
}
else if ($query === true)
{
switch (strtoupper(substr($sql, 0, strpos($sql, ' '))))
{
case 'INSERT':
return mysql_insert_id($this->db);
break;
case 'UPDATE':
case 'DELETE':
return mysql_affected_rows($this->db);
break;
}
return true;
}
}
return false;
}
function Quote($string)
{
if (get_magic_quotes_gpc() === true)
{
$string = stripslashes($string);
}
return '\'' . mysql_real_escape_string(trim($string), $this->db) . '\'';
}
}
$db = new MySQL('test', 'root', '');
var_dump($db->Query('INSERT ...'));
var_dump($db->Query('DELETE ...'));
var_dump($db->Query('UPDATE ...'));
echo '<pre>';
print_r($db->Query('SELECT ...'));
echo '</pre>';
?>
Hello ppl i'm new to php\\OOP and i'm trying to build a simple wrapper.
Any adice refactoring would be nice
Thank you.