PHP Class - Database operations (MySQL)

Very small class I have written to speed up basic Database operations. This works just fine with pretty simple db manipulations like SELECT, INSERT, UPDATE and DELETE, also known as CRUD operations.

CRUD stands for Create Read Update Delete.



<?php
class postActions {

    
/**        add / insert new rows        **/
    
function insert2db($f_table$f_records)
    {
        
$f_count 0;
        foreach (
$f_records as $key => $val)
        {
            if (
$f_count == 0) {
              
$f_fields $key;
              if (
$val == ''$f_values "''"; else $f_values "'" $val "'";
            }
            else {
              
$f_fields .= ", " $key;
              if (
$val == ''$f_values .= ", ''"; else $f_values .= ", '" $val "'";
            }
        
$f_count++;
        }
        
$query mysql_query("INSERT INTO " $f_table " (" $f_fields ") VALUES (" $f_values ")") or die (mysql_error());
    }


    
/**        select from table        **/
    
function selectfromdb ($f_what$f_table$f_condition=''$f_ekstra='')
    {
      if (
$f_condition != ''$f_condition ' where ' $f_condition;
      if (
$f_ekstra != ''$f_ekstra ' ' $f_ekstra;

      
$query mysql_query("SELECT " $f_what " FROM " $f_table $f_condition $f_ekstra "") or die (mysql_error());
      return 
$query;
    }

    
/**        update table        **/
    
function updatedb($f_table$f_records$f_condition)
    {
        
$f_count 0;
        foreach (
$f_records as $key => $val)
        {
            if (
$f_count == 0) {
            
$f_fields $key " = '" $val "'";
        }
            else {
            
$f_fields .= ", " $key " = '" $val "'";
        }

        
$f_count++;
        }
        
$query mysql_query("UPDATE " $f_table " SET " $f_fields " where " $f_condition "") or die(mysql_error());
    }


    
/**        delete rows        **/
    
function deletefromdb($f_table$f_condition)
    {
      
$query mysql_query("DELETE from " $f_table " where " $f_condition "") or die (mysql_error());
    }
}
?>

Usage

First we need to initialize the class:


<?php
$action 
= new postActions();
/*  ***   $action can be replaced with any name of your liking   ***  */
?>

For the demonstration purposes let's assume there's a DB with a table Profile. Profile has only three columns: ID, FirstName and LastName.

INSERT demo



<?php
    $insert_array 
= array(
     
'FirstName' => $_POST['FirstName'],
     
'LastName' => $_POST['LastName'],
    );
    
$action->insert2db('Profile'$insert_array);
?>

SELECT demo

Select all:



<?php
$q 
$action->selectfromdb('*''Profile');
?>
Select all and list them in descending order by last name:


<?php
$q 
$action->selectfromdb('*''Profile''''ORDER BY LastName DESC');
?>
Select only a particular ID:


<?php
$q 
$action->selectfromdb('*''Profile''id = \'4\'');
?>
Once we've selected what we wanted, the rest proceeds as a regular PHP / MySQL syntax:


<?php
while($row mysql_fetch_array($q)) {
..
..
..
}
?>

UPDATE demo



<?php
    $update_array 
= array(
     
'FirstName' => $_POST['FirstName'],
     
'LastName' => $_POST['LastName'],
    );
    
$action->updatedb('Profile'$update_array'id = \'' $_GET['inputFieldsValue'] . '\'');
?>

DELETE demo



<?php
$action
->deletefromdb('Profile''id = \'' $_GET['inputFieldsValue'] . '\'');
?>
And that's it!
Scroll to Top