User Tag List

Results 1 to 5 of 5

Thread: [PHP] DbLib

  1. #1

    Default [PHP] DbLib

    This is the database class I wrote to make my work with databases comfortable and reduce the code size of my scripts. The class is called simply DbLib. You can support different dbs by writing new derived classes, since there is only MySQL support. Feel free to write new dbs extensions for it, just remember to credit Saibal. :]

    PHP Code:
    <?php
    ///////////////////////////////////////////////////////////////////////////////
    // DbLib base class defines the basic operations. You can support
    // different dbs by writing a new derived class

    class DbLib
    {
        
        var        
    $mHost        'localhost';
        var        
    $mPort        =  3306;
        var        
    $mUser        'root';
        var        
    $mPassword    'password';
        var        
    $mDatabase    'database';
        var         
    $defcharset     'latin1';
        var        
    $mSocket;
        var        
    $mResult;
        var        
    $mRow;


        function 
    OpenConnection()    {    $this->mSocket  =  1;        }
        function 
    SelectDatabase()    {    $this->IsConnected();        }
        function 
    DbQuery()        {    $this->IsConnected();        }
        function 
    Query()        {    $this->IsConnected();        }
        function 
    GetResult()    {    $this->IsConnected();        }
        function 
    GetNumRows()    {    $this->IsConnected();        }
        function 
    GetNextRecord()    {    $this->IsConnected();        }
        function 
    GetField()        {    $this->IsConnected();        }
        function 
    FreeResult()    {    $this->IsConnected();        }
        function 
    GetAffectedRows()    {    $this->IsConnected();        }
        function 
    GetLastID()    {    $this->IsConnected();        }
        function 
    CloseConnection()    {    $this->IsConnected();        }
        function 
    GetError()        {    $this->IsConnected();        }
        function 
    SqlCorrection()     {       $this->IsConnected();        }
        function 
    __wakeup()        {    $this->OpenConnection();    }
        function 
    __sleep()        {    $this->CloseConnection();    }
        function 
    IsConnected()
        {
        if (!
    $this->mSocket) die('Not connected to database server');
        }
        
    }
        
    ///////////////////////////////////////////////////////////////////////////////
    // MySQL database support
    class DbLibMySQL extends DbLib
    {
        
        function 
    DbLibMySQL($host 0$port 0$user 0$password 0$database 0
        {
        if (
    $host)      $this->mHost      $host;
        if (
    $port)      $this->mPort      $port;
        if (
    $user)      $this->mUser      $user;
        if (
    $password)  $this->mPassword  $password;
        if (
    $database)  $this->mDatabase  $database;
        }

        
        function 
    OpenConnection()
        {
        
    parent::OpenConnection();
        
    $this->mSocket = @mysql_connect($this->mHost ':' $this->mPort$this->mUser$this->mPassword)
            or die(
    $this->GetError());
        @
    mysql_select_db($this->mDatabase,$this->mSocket)
            or die(
    $this->GetError());
        
    //$this->DbQuery("SET NAMES '{$this->DefCharset}'");
        
    return $this->mSocket;
        }
        

        function 
    SelectDatabase($database '')    
        {
        
    parent::SelectDatabase();
        @
    mysql_select_db($database,$this->mSocket) or die($this->GetError());
        
    $this->mDatabase $database;
        }


        function 
    DbQuery($query ''
        {
        
    parent::DbQuery();
        @
    mysql_unbuffered_query($query,$this->mSocket) or die($this->GetError());
        }


        function 
    Query($query '')
        {
        
    parent::Query();
        (
    $this->mResult = @mysql_query($query,$this->mSocket)) or die($this->GetError());
        return 
    $this->mResult;
        }


        function 
    GetResult($row 0$coll 0$result 0)    
        {
        
    parent::GetResult();
        (
    $result) ? $temp_result $result $temp_result $this->mResult;

        if (!
    is_resource($temp_result))           die('Not MySQL resource while getting results');
        return @
    mysql_result($temp_result,$row,$coll);
        }

        
        function 
    GetNumRows($result 0)
        {
        
    parent::GetNumRows();
        (
    $result) ? $temp_result $result $temp_result $this->mResult;

        if (!
    is_resource($temp_result))           die('Not MySQL resource while getting results');    
        return @
    mysql_num_rows($temp_result);
        }

        function 
    GetAffectedRows()
        {
        
    parent::GetAffectedRows();
        return @
    mysql_affected_rows($this->mSocket);
        }

        function 
    GetLastID()
        {
        
    parent::GetLastID();
        return @
    mysql_insert_id($this->mSocket);
        }
        
        function 
    GetNextRecord($result 0)
        {
        
    parent::GetNextRecord();
        (
    $result) ? $temp_result $result $temp_result $this->mResult;

        if (!
    is_resource($temp_result))           die('Not MySQL resource while getting results');
        
            
    $this->mRow = @mysql_fetch_assoc($temp_result);
            return 
    $this->mRow != FALSE;
        }

        function 
    GetField($field ''
        {
        
    parent::GetField();
            return 
    $this->mRow[$field];
        }


        function 
    FreeResult($result 0)
        {
        
    parent::FreeResult();
        if (
    $result$temp_result $result;
        else          
    $temp_result $this->mResult;

        if (!
    is_resource($temp_result))             die('Not MySQL resource while freeing results');
        @
    mysql_free_result($temp_result);
        }


        function 
    SqlCorrection($string
        {
        
    parent::SqlCorrection();
        return @
    mysql_real_escape_string($string);    
        }
        

        function 
    GetError() 
        {
        
    parent::GetError();
        return @
    mysql_error();
        }

       
        function 
    CloseConnection()
        {
        
    parent::CloseConnection();
        @
    mysql_close($this->mSocket) or die('There is no valid MySQL-Link resource');
        }

    }
    ?>

  2. #2
    Member soul's Avatar
    Join Date
    Apr 2008
    Location
    Dhaka, Bangladesh, Bangladesh
    Posts
    474

    Default

    if I write wht will happen ?

    I'll start again
    And whatever pain may come
    Today this ends
    I'm forgiving what I've done

  3. #3

    Default

    It's only useful if you're running a dedicated server with MySQL support

  4. #4

    Default

    i have this one...think its better
    Code:
    <?php
    //inseert data in table
    function insert_data($table, $data_array)
    {	
    global $db_database, $tbl_prefix;
    	$table = $tbl_prefix . $table;
    
    	
    	$result = mysql_list_fields($db_database, $table);
    	
    	$query = "INSERT INTO `$table` VALUES (";
    	
    	for($i=0 ; $i<mysql_num_fields($result) ; $i++)
    		{
    		$field_name = mysql_field_name($result, $i);
    		$query .= "'$data_array[$field_name]'";
    		
    		if($i < mysql_num_fields($result)-1)
    			{
    			$query .= ", ";
    			}
    		else{
    			$query .= ")";
    			}
    		}
    		
    	//echo $query."<BR>";
    		
    	if(mysql_query($query))
    		return 1;
    	else
    		return 0;
    }
    
    
    
    function update_data($table, $data_array, $where)
    {	
    	
    	global $db_database, $tbl_prefix;
    	$table = $tbl_prefix . $table;
    	
    	$result = mysql_list_fields("$db_database", "$table");
    	
    	$cnt_index = 0;
    	
    	for($i=0; $i < mysql_num_fields($result); $i++ )
    	{
    		$field_name =  mysql_field_name($result,$i );
    	   
    		if($data_array[$field_name] == "")
    	   		{
    			continue;
    			}
    		else	 
    			{
    			$cnt_index++;
    			}
    	}
    	
    	
    	
    	$query = "UPDATE `$table` SET";
    	
    	$cnt_array=0;
    	
    	for($i=0; $i < mysql_num_fields($result); $i++ )
    	{
    		$field_name =  mysql_field_name($result,$i );
    	   
    		if($data_array[$field_name] == "")
    	   		{
    			continue;
    			}
    		else	 
    			{
    			$query .= " `$field_name` = '$data_array[$field_name]'";
    			$cnt_array++;
    			}
    			
    		
    		if($cnt_array == $cnt_index)  //count($data_array))
    			{
    			break;
    			}
    		else
    			{
    			$query .= ",";
    			}
    	   
    	}
    	
    	$query .= " $where";
    	
    	//echo $query."<BR>";
    	
    	if(mysql_query($query))
    		return 1;
    	else
    		return 0;
    	
    }
    
    
    
    function delete_data($table, $where)
    {
    	global $db_database, $tbl_prefix;
    	$table = $tbl_prefix . $table;
    	
    	$query = "DELETE FROM `$table` $where";
    	
    	//echo $query."<BR>";
    		
    	$check = mysql_query($query);		  
    	if($check)
    		return 1;
    	else
    		return 0;
    }
    
    
    //select data frm table
    function select_data($fetch, $table, $where, $process)
    {
    	global $db_database, $tbl_prefix;
    	
    	$table = $tbl_prefix . $table;
    	
    	
    	$query = "SELECT $fetch FROM `$table` $where";
    	$result = mysql_query($query);
    	
    	//echo $query."<BR>";
    	
    	switch($process)
    	{
    		// SELECT ALL DATA OF TABLE [2D ARRAY]
    		/*---------------------------------------------------------*/
    		case "all_data":
    		{
    			$i=0;
    			if(mysql_num_rows($result)!=0)
    			{
    				while ($data = mysql_fetch_array($result))
    				{
    					$result1 = mysql_list_fields("$db_database", "$table");
    					for($j=0; $j<mysql_num_fields($result1) ; $j++ )
    					{
    						$field_name =  mysql_field_name($result1,$j );
    						$return_array[$i][$field_name] = $data[$field_name];
    					}
    					$i++;
    				}
    				return $return_array;	
    			}
    			break;
    		}
    	
    
    		// SELECT (SINGLE) ROW DATA OF TABLE [1D ARRAY_ROW]
    		/*---------------------------------------------------------*/
    		case "row_data":
    		{
    			if(mysql_num_rows($result)!=0)
    			{
    				$data = mysql_fetch_array($result);
    				$result = mysql_list_fields("$db_database", "$table");
    				for($i=0; $i < mysql_num_fields($result); $i++ )
    				{
    					$field_name =  mysql_field_name($result,$i );
    					$return_array[$field_name] = $data[$field_name];
    				}
    				return 	$return_array;
    			}
    			break;
    		}
    
    
    		// SELECT (SINGLE) COLUMN DATA OF TABLE [1D ARRAY_COLUMN]
    		/*---------------------------------------------------------*/
    		case "column_data":
    		{
    			$i=0;
    			if(mysql_num_rows($result)!=0)
    			{
    				while ($data = mysql_fetch_row($result))
    				{
    					$return_array[$i] = $data[0];
    					$i++;
    					
    					//echo $data[0]."<BR>";
    				}
    				return $return_array;
    			}
    			break;
    		}
    
    
    		// SELECT SINGLE DATA OF TABLE [SINGLE VARIABLE]
    		/*---------------------------------------------------------*/
    		case "single_data":
    		{
    			$data = mysql_fetch_row($result);
    			
    			if(mysql_num_rows($result)==1)
    				return $data[0];
    			else
    				return 0;
    			break;
    		}
    				
    	}//end of switch	
    	
    }
    
    
    //run sql
    function run_sql($query)
    {
    	global $db_database, $tbl_prefix;
    	
    	$table = $tbl_prefix . $table;
    	
    	$result = mysql_query($query);
    	
    	//echo $query."<BR>";
    	
    	$data = mysql_fetch_row($result);
    	
    	if(mysql_num_rows($result)==1)
    		return $data[0];
    	else
    		return 0;
    }
    
    
    //	RADIO BUTTON
    /*---------------------------------------------------------*/
    function radio_button($rcd_array, $id, $show, $select_index, $radio_name, $disabled){
    	$i=count($rcd_array);
    	for($j=0; $j<$i; $j++){
    		if($rcd_array[$j][$id] == $select_index)
    			echo "<input type='radio' name='". $radio_name ."' value='". $rcd_array[$j][$id] ."' checked ". $disabled ."> " . $rcd_array[$j][$show];
    		else
    			echo "<input type='radio' name='". $radio_name ."' value='". $rcd_array[$j][$id] ."' ". $disabled ."> " . $rcd_array[$j][$show];
    	}
    }
    
    
    //	COMBO BOX
    /*---------------------------------------------------------*/
    function combo_box($rcd_array, $id, $show, $select_index){
    	for($j=0; $j<count($rcd_array); $j++){
    		if($rcd_array[$j][$id] == $select_index)
    			print("<option value='".$rcd_array[$j][$id]."' selected >".$rcd_array[$j][$show]."</option>\n");
    		else
    			print("<option value='".$rcd_array[$j][$id]."'>".$rcd_array[$j][$show]."</option>\n");
    	}
    }
    
    
    //	Empty checking
    /*---------------------------------------------------------*/
    function empty_check($array){
    	for($i=0 ; $i<count($array) ; $i++){
    		if(empty($array[$i])){
    			return 1;
    		}
    	}
    }
    
    
    //	NUMBER checking
    /*---------------------------------------------------------*/
    function number_check($value)
    {
    		if(is_numeric($value) == TRUE)
    			{
    			return 1;
    			}
    		else
    			{
    			return 0;	
    			}
    }
    
    
    //	TIME CONVERTION
    function time_convert($time)
    {
    	if(strlen($time) == 5)
    	{
    		$case = "24 hrs";
    	}
    	elseif(strlen($time) == 8 && substr($time, 0, 2)<=12 && substr($time, 0, 2)>0 && (preg_match("/\bAM\b/", $time) || preg_match("/\bPM\b/", $time) ) )
    	{
    		$case = "12 hrs";
    	}	
    		
    	switch($case)
    	{
    		case "24 hrs":
    		{
    			$hour = substr($time, 0, 2);
    			
    			if($hour > 11)
    				$AMPM = "PM";
    			else
    				$AMPM = "AM";
    			
    			if($hour == '00' || $hour == '12')
    				$ampm_hr = 12;
    			else
    				$ampm_hr = ($hour*1) % 12;
    				
    			if($ampm_hr < 10)
    				$ampm_hr = '0' . $ampm_hr;	
    			
    			$AMPM_time = $ampm_hr . substr($time, 2, 3) . " " . $AMPM;
    			
    			return $AMPM_time;
    			break;
    		}
    		case "12 hrs":
    		{
    			$head = substr($time, 0, 2);
    			$tail = substr($time, 6, 2);
    			
    			if($tail == "AM")
    			{
    				$head = $head % 12;		
    			}
    			else
    			{
    				$head = $head % 12 + 12;
    			}
    			
    			if($head < 10)
    				$head = '0' . $head;	
    			
    			$return_time = $head . substr($time, 2, 3);
    			
    			return $return_time;
    			break;
    		}
    		default:
    		{
    			return "error";
    			break;
    		}
    		
    	}	
    		
    }
    
    
    
    //	EMAIL ADDRESS FORMAT CHACKING
    /*---------------------------------------------------------*/
    function  email_format($address){
    	$pattern = "/^[A-z0-9\._-]+" . "@" . "[A-z0-9][A-z0-9-]*" . "(\.[A-z0-9_-]+)*" . "\.([A-z]{2,6})$/";
    	return preg_match($pattern, $address);
    }
    
    
    //	EMAIL SENDING
    // Array Index required: [to_name], [to_email], [from_name], [from_email], [subject], [message]
    /*---------------------------------------------------------*/
    function email($email){
    	// To send HTML mail, the Content-type header must be set
    	$headers  = 'MIME-Version: 1.0'."\r\n";
    	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    
    	// Additional headers
    	$headers .= 'To: ' . $email[to_name] . '<' . $email[to_email] . '>' . "\r\n";
    	$headers .= 'From: ' . $email[from_name] . '<' . $email[from_email] . '>' . "\r\n";
    	$headers .= 'X-Mailer: Initbd/' . phpversion();
    
    	if(mail($email[to_email], $email[subject], $email[message], $headers))
    		return 1;
    	else
    		return 0;
    }
    
    
    //////////////////////////////  FETCH TABLE INFORMATION (23-05-2007)  //////////////////////////////////	
    
    function table_info($table)
    	{
    			$query = "SHOW FIELDS FROM $table";
    	
    	//echo $query."<br>";
    			  	
    	$i=0;
    		
    	$result = mysql_query($query);
    
    	if(mysql_num_rows($result)!=0)
    		{
    			while ($data = mysql_fetch_row($result))
    				{
    				$return_array[$i][tab_field] = $data[0];
    				$return_array[$i][tab_type] = $data[1];
    				$return_array[$i][tab_null] = $data[2];
    				$return_array[$i][tab_key] = $data[3];
    				$return_array[$i][tab_default] = $data[4];
    				$return_array[$i][tab_extra] = $data[5];				
    
    				$i++;
    				}
    			return $return_array;	
    		}
    	else
    		return 0;	
    
       	}
    	
    //////////////////////////////  FETCH TABLE DATA (24-05-2007)  //////////////////////////////////	
    
    function table_data($table)
    	{
    		$query = "SELECT *
    				  FROM `$table`";
    	
    	//echo $query."<br>";
    			  	
    	$i=0;
    		
    	$result = mysql_query($query);
        
    	if(mysql_num_rows($result)!=0)
    		{
    			while ($data = mysql_fetch_row($result))
    				{
    				for($j=0; $j<count($data) ; $j++ )
    					{
    						$return_array[$i][$j] = $data[$j];
    					}
    				$i++;
    				}
    			return $return_array;	
    		}
        
       	}
    			
    //////////////////////////////  FETCH TABLE FIELD NAME (13-10-2007)  //////////////////////////////////	
    
    function table_field($table)
    {
    global $database;
    $result = mysql_list_fields("$database", "$table");
    
    for($i=0; $i < mysql_num_fields($result); $i++ )
    	{
    		$return_array[$i] =  mysql_field_name($result,$i );
    	}
    
    return $return_array;
    }
    
    ?>
    I think I can, can I think, then I think I can ...
    [SIGPIC][/SIGPIC]

  5. #5

    Default

    Ghost, did you make that yourself? Or did you base it off of someone else's?

    You have some typos in there, so it has to be fixed. Also, it's much longer and mine is based off of variables and functions which makes it easier custom commands.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 0.17765 seconds with 13 queries.