Skip to content

Instantly share code, notes, and snippets.

@kodi
Created May 17, 2009 19:57
Show Gist options
  • Save kodi/113144 to your computer and use it in GitHub Desktop.
Save kodi/113144 to your computer and use it in GitHub Desktop.
<?php
/*
* simple mysql database class
CREATE TABLE IF NOT EXISTS `kategorije` (
`id` mediumint(8) unsigned NOT NULL auto_increment,
`name` varchar(128) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=3 ;
--
-- Dumping data for table `kategorije`
--
INSERT INTO `kategorije` (`id`, `name`) VALUES
(1, 'Nokia'),
(2, 'Siemens');
*/
// ovo bi u normalnim okolnostima islo u config fajl
define('DB_Server','localhost');
define('DB_User','root');
define('DB_Pass','');
define('DB_Database','test'); //ime baze
//kraj konfiguracije
class Db{
private $dbh;
private $dbc;
function Db($auto=true){
if($auto===true){
$this->connect();
}
}
function connect($host=DB_Server,$user=DB_User,$pass=DB_Pass,$database=DB_Database){
if(!$this->dbc = mysql_connect($host,$user,$pass)){
die('ERROR CONNECTING TO THE DATABASE: '.mysql_error());
}
$this->dbh = mysql_select_db($database,$this->dbc);
return $this->dbc;
}
function Execute($sql){
if(!$result=mysql_query($sql,$this->dbc)){
die (mysql_error().' <br/>SQL Query: '.$this->formatSql($sql));
}
$this->numRows=mysql_num_rows($result);
$this->last_id=mysql_insert_id();
return $result;
}
function Update ($SQL){
$rez=$this->ExecuteReplace($SQL);
return $rez;
}
function Delete ($SQL){
$rez=$this->ExecuteReplace($SQL);
return $rez;
}
function Insert ($SQL){
$rez=$this->ExecuteReplace($SQL);
return $rez;
}
// koristi se za INSERT, UPDATE I DELETE, jer vraca drugaciji broj affected rows, kao i last inserted id
function ExecuteReplace($sql){
if(!$result=mysql_query($sql,$this->dbc)){
die(mysql_error() .'<br/>SQL Query: '.$this->formatSql($sql));
}
$this->numRows=$result;
$this->last_id=mysql_insert_id();
return $result;
}
function getArray($sql){
$result=array();
$stmt=$this->dbh->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch($this->fetch_mode)) {
$result[]=$row;
}
return $result;
}
function getNumRows($q){
$numr=mysql_num_rows($q);
return $numr;
}
function getAssoc($SQL){
$result=$this->Execute($SQL);
$out=Array();
while ($row = mysql_fetch_assoc($result)) {
$out[]= $row;
}
return $out;
}
function getRow($SQL){
$result=$this->Execute($SQL);
$out=mysql_fetch_assoc($result);
return $out;
}
function formatSql($sql){
$e = explode("\n",$sql);
$color = "ffff00";
$l = $sql;
$l = str_replace("SELECT ", "<br /><font color='$color'><b>SELECT </b></font><br \> ",$l);
$l = str_replace("FROM ", "<br/><font color='$color'><b>FROM </b></font><br \>",$l);
$l = str_replace("LEFT JOIN ", "<br \><font color='$color'><b> LEFT JOIN </b></font>",$l);
$l = str_replace("INNER JOIN ", "<br \><font color='$color'><b> INNER JOIN </b></font>",$l);
$l = str_replace("WHERE ", "<br \><font color='$color'><b> WHERE </b></font>",$l);
$l = str_replace("GROUP BY ", "<br \><font color='$color'><b> GROUP BY </b></font>",$l);
$l = str_replace(" HAVING ", "<br \><font color='$color'><b> HAVING </b></font>",$l);
$l = str_replace(" AS ", "<font color='$color'><b> AS </b></font><br \> ",$l);
$l = str_replace("ON ", "<br><font color='$color'><b> ON </b></font>",$l);
$l = str_replace("ORDER BY ", "<br /><font color='$color'><b> ORDER BY </b></font>",$l);
$l = str_replace("LIMIT ", "<font color='$color'><b> LIMIT </b></font><br \>",$l);
$l = str_replace("OFFSET ", "<font color='$color'><b> OFFSET </b></font><br \>",$l);
$l = str_replace(" ", "<dd>",$l);
return $l;
}
function closeConnection($link){
mysql_close($this->dbc);
}
}
$db=new Db();
$jedanRow=$db->getRow("select * from kategorije where id=1");
$viseRezultata=$db->getAssoc("select * from kategorije");
echo '<pre>';
print_r($jedanRow);
print_r($viseRezultata);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment