Module Template [database.class.php]

tiba-tiba pengen banget nulis tentang Coding PHP yang selama ini dibuat, ngga tau kenapa rasa RUGI tuh ngga kerasa, apa ini gara-gara ngikut aliran Open Source ato cuma Iseng-iseng.. Bodo’ amat ah

File Name : database.class.php

// file ini biasa aku pake buat nge-handle operasi database. tapi buat pembahasannya mungkin lain kali aja, soalnya gi gawe  nich…


<?php
session_start();
class mySql {
private $DBhost;                  //your internet address of the mySQL database your going to use.
private $DBusername;                 //your mysql User name of the mySQL database your going to use.
private $DBpassword;                 //your mysql password of the mySQL database your going to use.
private $DBname;                     //name of database
private $link_id;
private $sql;
private $result;
private $num_rows;
private $row;
private $l_field;
private $field_define;
private $NOL;

function mySql(){//__construct
$this->DBhost = “localhost”;
$this->DBusername = “root”;
$this->DBpassword = “”;
$this->DBname = “ecommerce”;
$this->NOL=”0″;
}
public function getDBuid(){
return $this->DBusername;
}
public function getDBhost(){
return $this->DBhost;
}
public function getDBpass(){
return $this->DBpassword;
}
public function getDBname(){
return $this->DBname;
}
public function openKoneksi(){
$this->link_id=mysql_connect($this->DBhost,$this->DBusername,$this->DBpassword);
return mysql_select_db($this->DBname,$this->link_id)or die(“UNABLE TO SELECT DATABASE”);
}
function execute($query){
$this->sql=$query;
$this->result=mysql_query($this->sql)or die(mysql_error().”<br>”.$query);
}

function triggerDate($thisTable, $isKey, $isValue){
$hari_ini=$_COOKIE["UNIX_TIME"];
$this-execute(“update $thisTable set dm=’$hari_ini’ where $isKey=’$isValue’”);
}
function deleteRows($isTable,$isCriteria){
return $this->execute(“DELETE FROM $isTable WHERE $isCriteria”) or die(mysql_error());
}
function updateRows($isTable,$isValues,$isCriteria){
return $this->execute(“UPDATE $isTable SET $isValues WHERE $isCriteria”);
}
function insertRows($isTable,$isValues){
$tmp=”INSERT INTO $isTable VALUES(“.$isValues.”)”;
return $this->execute($tmp);
}
function getArray(){
$this->row=mysql_fetch_array($this->result,MYSQL_NUM);
return $this->row;
}
function getField($thisField,$thisTable,$thisCriteria,$thisValue){
$Q=$this->execute(“SELECT $thisField FROM $thisTable WHERE $thisCriteria=’$thisValue’”);
$R=$this->getArray();
return $R[0];
}
function record_count(){
$this->num_rows=mysql_num_rows($this->result);
return $this->num_rows;
}
function list_field($table_name){
$this->l_field=mysql_list_fields($this->DBname,$table_name);
$this->l_field;
}
function num_fields(){
$this->field_count=mysql_num_fields($this->l_field);
return $this->field_count;
}
function back_up_this($into_file){
$into_file = fopen(“backup\\”.$into_file,”w+”);
$i=0;$x=0;$counter=0;
$this->execute(“show tables”);
$list_table=”";
while($row=$this->getArray()){
$list_table.=$row['0'].”#”;

}
$list_table=substr($list_table,0,strlen($list_table)-1);
$sTable=explode(“#”,$list_table);
$cBackUp=count($sTable);
for($counter=0;$counter<$cBackUp;$counter++)
{
$this->execute(” Select * From “.$sTable[$counter]);
$this->list_field($sTable[$counter]);
$field_count=$this->num_fields();
//mysql_num_fields($this->result)
for($i=0;$i<$this->record_count();$i++){
while($R=$this->getArray()){
$X = “Insert Into $sTable[$counter] Values(“;
for($x=0;$x<$field_count;$x++){
if ($x==($field_count-1)){
$X .=”‘$R[$x]‘”;
}else{
$X .= “‘$R[$x]‘,”;
}
}
$X .= “)\n”;
fwrite($into_file,$X);
}
}
}
fclose($into_file);
}
function load_to_combo($r,$data_showed){
$var=”";
$tampil=”";
$j_array=count(explode(“#”,$r));
while($data=$this->getArray()){
$var.=”<option value=\”";
for($i=0;$i<=$j_array;$i++){
$var.=$data[$i].”#”;
}
$var.=”\”>$data[$data_showed]</option>”;
}

return $var;
}

function restore_this($from_file){
copy($from_file,”backup\\i”.$from_file);
$fp = fopen(“backup\\$from_file”, “r”);
while(!feof($fp)){
$x = fgets($fp, 4096);
$this->execute($x);
}
fclose($fp);
}
function auto_number($field,$table,$key,$Parse,$Digit_Count){
$this->NOL=”0″;
$this->execute(“Select $field from $table where $key like ‘$Parse%’ order by $key DESC”);
$counter=2;
if($this->record_count()==0)
{
while($counter < $Digit_Count)
{
$this->NOL=”0″.$this->NOL;
$counter++;
}
return $Parse.$this->NOL.”1″;
}
else
{
$R = $this->getArray();
$K = sprintf(“%d”,substr($R[0],-$Digit_Count));
$K = $K + 1;
$L = $K;
while(strlen($L)!=$Digit_Count)
{
$L = $this->NOL.$L;
}
return $Parse.$L;
}
}
}
?>

Leave a Reply