article

Sunday, November 8, 2015

Add Edit Delete Using PHP Class

Add Edit Delete Using PHP Class






 
<?php
$db['host']     = 'localhost';   //Your database host, I.E. localhost
$db['username'] = 'root';        //Your database username
$db['password'] = '';            //Your database password
$db['db']       = 'mycmsdb';   //Your database name
$db['prefix']   = '';            //Your table prefix, can be left blank

class MySQLDB
{
        var $dbhost;
        var $dbuser;
        var $dbpass;
        var $dbname;
        var $dblink;
        var $qrystr;
        var $result;
        var $dbprefix;
        
        function MySQLDB($dbhost, $dbuser, $dbpass, $dbname, $dbprefix)
        {
                $this->dbhost=$dbhost;
                $this->dbuser=$dbuser;
                $this->dbpass=$dbpass;
                $this->dbname=$dbname;
                $this->dbprefix=$dbprefix;
        }
        
        function connectdb()
        {
                $this->dblink=mysql_connect($this->dbhost,$this->dbuser,$this->dbpass) or die($this->show_error());
        }
        
        function selectdb()
        {
                mysql_select_db($this->dbname) or die($this->show_error());
        }
        
        function show_error()
        {
                print mysql_error($this->dblink);
        }
        
        function query($qry="")
        {
                if(!empty($qry)) $this->qrystr=$qry;
                if(empty($this->qrystr)) die("Error: Query string is empty.");
                else $this->result=mysql_query($this->qrystr,$this->dblink) or die($this->show_error());                
        }
        
        function setqrystr($qry)
        {
                $this->qrystr=$qry;
        }
        
        function get_insert_id()
        {
                return mysql_insert_id($this->dblink);
        }
        
        function getrow()
        {
                return mysql_fetch_row($this->result);
        }
        
        function getarr()
        {
                return mysql_fetch_array($this->result,MYSQL_ASSOC);
        }
        
        function getobj()
        {
                return mysql_fetch_object($this->result);
        }
        
        function getaffectedrows()
        {
                return mysql_affected_rows($this->dblink);
        }
        
        function getrownum()
        {
                return mysql_num_rows($this->result);
        }
        
        function freeresult()
        {
                mysql_free_result($this->result);
        }
        
        function closedb()
        {
                mysql_close($this->dblink);
        }
        
        function __destruct()
        {
                mysql_close($this->dblink);
        }
        
        function tb($tablename)
        {
                if(empty($this->dbprefix))        return $tablename;
                else return $this->dbprefix."_".$tablename;
        }
}
//Hostname,Username,Password,Database,table prefix
$db=new MySQLDB($db['host'], $db['username'], $db['password'], $db['db'], $db['prefix']);
$db->connectdb();
$db->selectdb();
?>

<?
//Examples
$qry="SELECT * FROM rme_bookings WHERE city='angeles'";
$db->query($qry);
$row=$db->getrow();
$maxtime=$row[0]; 
echo $maxtime;
//Udate
$qry="UPDATE ".$db->tb("admin")." SET uname='$uname', pwd='$pwd', email='$email' WHERE uid=$auid";
$db->query($qry);
 if($db->getaffectedrows()==0) $err[0]="Nothing altered! Try again.";
 else  $err[0]="Profile updated successfully."; 

$qry="SELECT conf_value FROM ".$db->tb("configuration")." WHERE conf_name='AUTO_FILE_DELETE'";
$db->query($qry);
$row=$db->getrow();
if($row[0]=="Yes")
{
 $now=time();
 $qry="SELECT dir, file_name FROM ".$db->tb("fileinfo")." WHERE expire_time<$now";
 $db->query($qry);
 while($row=$db->getrow())
 {
  @unlink("uploads/".$row[1]."/".$row[2]);
  @rmdir("uploads/".$row[1]);
 }
 $qry="DELETE FROM ".$db->tb("fileinfo")." WHERE expire_time<$now";
 $db->query($qry);
}

 $qry="SELECT uname, pwd, email FROM ".$db->tb("admin")." WHERE uid=$auid";
$db->query($qry);
 $row=$db->getrow();
 $uname=$row[0];
$pwd=$row[1];
 $email=$row[2];
  
$qry="SELECT * FROM ".$db->tb("fileinfo")." ORDER BY upload_time DESC";
$db->query($qry);
while($row=$db->getarr())
{
 $status="Ok";
 $idkey = $row["idkey"];
 if($row["no_of_dwnld"]>=$row["max_dwnld"]) $status="Count Exceeded";
 if($row["expire_time"]<time()) $status="Expired";
 if($row["link_status"]==0) $status="Suspended";
}   

$qry="DELETE FROM ".$db->tb("adminlog")." WHERE uid=".$row[0]." and timein=".$row1[0];
$db->query($qry);  

$qry="INSERT INTO ".$db->tb("adminlog")."(uid,timein,ip) VALUES(".$row[0].",".time().",'".$_SERVER['REMOTE_ADDR']."')";
$db->query($qry);

$db->query("UPDATE ".$db->tb("admin")." SET `pwd`='".$md5."'");
$row=$db->getrow();
?>

Related Post