HotelUser
28-08-2010, 10:56 PM
Hey there!
It's not so much that I'm unhappy with this SQL class, or that it's dysfunctional (although if you have an improvement for it I'd love to hear about it), I'd just like to compare and contrast with with other user's coding techniques. I suppose I could have posted this on Stack Overflow but I would really like to see how other's code here who I could probably relate to more:
$g_count = 0;
class mysqlDb
{
public $con;
public $debug;
public $count;
function __construct($host,$username,$password,$database)
{
if($GLOBALS['g_count'] == 0)
{
$this->con = mysql_connect($host,$username,$password);
mysql_select_db($database, $this->con);
}
$GLOBALS['g_count']++;
}
function kill()
{
$GLOBALS['g_count']--;
if($GLOBALS['g_count'] == 0)
mysql_close($this->con);
}
function debugOn()
{
$this->debug = true;
}
function debugOff()
{
$this->debug = false;
}
function select($query,&$array)
{
$c = 0;
$result = mysql_query("SELECT ".$query);
$this->count = mysql_num_rows($result);
if($this->debug == true)
echo "SELECT ".$query;
while($row = mysql_fetch_array($result))
{
foreach($row as $id => $value)
{
$array[$c][$id] = $value;
}
$c++;
}
}
function update($update, $where,$array, $math = NULL)
{
if(!is_array($math))
{
foreach($array as $id => $value)
{
mysql_query("UPDATE {$update} SET `{$id}` = '{$value}' WHERE {$where}");
if($this->debug == true)
echo "UPDATE {$update} SET `{$id}` = '{$value}' WHERE {$where}";
}
}else{
foreach($array as $id => $value)
{
if(isset($math[$id]))
$statement = "UPDATE {$update} SET {$id} = {$id} {$math[$id]} {$value} WHERE {$where}";
else
$statement = "UPDATE {$update} SET {$id} = '{$value}' WHERE {$where}";
mysql_query($statement);
if($this->debug == true)
echo $statement;
}
}
}
function delete($t, $w)
{
mysql_query("DELETE FROM `{$t}` WHERE {$w}");
if($this->debug == true)
echo "DELETE FROM `{$t}` WHERE {$w}<br><br>";
}
function insert($where, $array)
{
$sql = "INSERT INTO `{$where}` (";
$sql2 = " VALUES (";
foreach($array as $id => $value){
$sql .= "`{$id}`, ";
$sql2 .= "'{$value}', ";
}
mysql_query(str_replace(', )',')',$sql.")") . str_replace(', )',')',$sql2.");"));
if($this->debug == true)
echo str_replace(', )',')',$sql.")") . str_replace(', )',')',$sql2.");")."<br><br>";
}
static function clean(&$x)
{
$x = mysql_real_escape_string($x);
}
static function cleanArray(&$x)
{
foreach($x as $id => $v)
$v = mysql_real_escape_string($v);
}
}
If anyone else here has a similar setup, or, would like to code their interpretation of how they would code a class to complete a similar task, please do!
It's not so much that I'm unhappy with this SQL class, or that it's dysfunctional (although if you have an improvement for it I'd love to hear about it), I'd just like to compare and contrast with with other user's coding techniques. I suppose I could have posted this on Stack Overflow but I would really like to see how other's code here who I could probably relate to more:
$g_count = 0;
class mysqlDb
{
public $con;
public $debug;
public $count;
function __construct($host,$username,$password,$database)
{
if($GLOBALS['g_count'] == 0)
{
$this->con = mysql_connect($host,$username,$password);
mysql_select_db($database, $this->con);
}
$GLOBALS['g_count']++;
}
function kill()
{
$GLOBALS['g_count']--;
if($GLOBALS['g_count'] == 0)
mysql_close($this->con);
}
function debugOn()
{
$this->debug = true;
}
function debugOff()
{
$this->debug = false;
}
function select($query,&$array)
{
$c = 0;
$result = mysql_query("SELECT ".$query);
$this->count = mysql_num_rows($result);
if($this->debug == true)
echo "SELECT ".$query;
while($row = mysql_fetch_array($result))
{
foreach($row as $id => $value)
{
$array[$c][$id] = $value;
}
$c++;
}
}
function update($update, $where,$array, $math = NULL)
{
if(!is_array($math))
{
foreach($array as $id => $value)
{
mysql_query("UPDATE {$update} SET `{$id}` = '{$value}' WHERE {$where}");
if($this->debug == true)
echo "UPDATE {$update} SET `{$id}` = '{$value}' WHERE {$where}";
}
}else{
foreach($array as $id => $value)
{
if(isset($math[$id]))
$statement = "UPDATE {$update} SET {$id} = {$id} {$math[$id]} {$value} WHERE {$where}";
else
$statement = "UPDATE {$update} SET {$id} = '{$value}' WHERE {$where}";
mysql_query($statement);
if($this->debug == true)
echo $statement;
}
}
}
function delete($t, $w)
{
mysql_query("DELETE FROM `{$t}` WHERE {$w}");
if($this->debug == true)
echo "DELETE FROM `{$t}` WHERE {$w}<br><br>";
}
function insert($where, $array)
{
$sql = "INSERT INTO `{$where}` (";
$sql2 = " VALUES (";
foreach($array as $id => $value){
$sql .= "`{$id}`, ";
$sql2 .= "'{$value}', ";
}
mysql_query(str_replace(', )',')',$sql.")") . str_replace(', )',')',$sql2.");"));
if($this->debug == true)
echo str_replace(', )',')',$sql.")") . str_replace(', )',')',$sql2.");")."<br><br>";
}
static function clean(&$x)
{
$x = mysql_real_escape_string($x);
}
static function cleanArray(&$x)
{
foreach($x as $id => $v)
$v = mysql_real_escape_string($v);
}
}
If anyone else here has a similar setup, or, would like to code their interpretation of how they would code a class to complete a similar task, please do!