Reusable PHP CRUD DATABASE FUNCTIONS

Today, We want to share with you Reusable PHP CRUD DATABASE FUNCTIONS.In this post we will show you TIME SAVING DATABASE FUNCTIONS, hear for insert update delete in php using classes we will give you demo and example for implement.In this post, we will learn about crud operations in php using oops with an example.

Reusable PHP CRUD DATABASE FUNCTIONS

There are the Following The simple About Reusable PHP CRUD DATABASE FUNCTIONS Full Information With Example and source code.

As I will cover this Post with live Working example to develop php oop crud pdo, so the sample php application with mysql for this example is following below.

PHP Reuse a MySQL object in function Examples

Read Also:  How to create an visitor counter with HTML & PHP?

RowUpdateData('my_any_table', $request_data, "WHERE id = '$id'");
RowDeleteData('my_db_table', "WHERE id = '$id'");

$request_data = array(
    'member_name' => $member_name,
    'sir_name' => $sir_name,
    'profile' => $profile,
    'address1' => $address1,
    'country' => $country,
    'city' => $city,
    'areacode' => $areacode,
    'phone' => $phone,
    'mobile' => $mobile,
    'fb_url' => $fb_url,
    'pay' => $pay,
    'title' => $title,
    'information' => $information,
    'reference' => $reference,
    'time' => time()
);

function addRowInsertData($db_tbl_name, $request_data)
{
    // retrieve the keys of the array (column titles)
    $fields = array_keys($request_data);

    // build the query
    $sql = "INSERT INTO ".$db_tbl_name."
    (`".implode('`,`', $fields)."`)
    VALUES('".implode("','", $request_data)."')";

    // run and return the query result resource
    return mysql_query($sql);
}

we would create a PHP delete function:

// the mysql query where clause is left optional incase the user wants to delete every row!
function RowDeleteData($db_tbl_name, $where_set='')
{
    // check for optional where clause
    $whereQry = '';
    if(!empty($where_set))
    {
        // check to display if the 'where' keyword exists
        if(substr(strtoupper(trim($where_set)), 0, 5) != 'WHERE')
        {
            // not found, add keyword
            $whereQry = " WHERE ".$where_set;
        } else
        {
            $whereQry = " ".trim($where_set);
        }
    }
    // build the SQL query
    $sql = "DELETE FROM ".$db_tbl_name.$whereQry;

    // run and return the query result resource
    return mysql_query($sql);
}

And then for PHP an update function:

Read Also:  PHP MySQL Database Connectivity Examples

// again where clause is left optional
function RowUpdateData($db_tbl_name, $request_data, $where_set='')
{
    // check for optional where clause
    $whereQry = '';
    if(!empty($where_set))
    {
        // check to see if the 'where' keyword exists
        if(substr(strtoupper(trim($where_set)), 0, 5) != 'WHERE')
        {
            // not found, add key word
            $whereQry = " WHERE ".$where_set;
        } else
        {
            $whereQry = " ".trim($where_set);
        }
    }
    // start the actual SQL statement
    $sql = "UPDATE ".$db_tbl_name." SET ";

    // loop and build the column /
    $sets = array();
    foreach($request_data as $column => $value)
    {
         $sets[] = "`".$column."` = '".$value."'";
    }
    $sql .= implode(', ', $sets);

    // append the where statement
    $sql .= $whereQry;

    // run and return the query result
    return mysql_query($sql);
}

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  PHP OOPS - MySQL connecting Insert update delete select Fetch Data

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Reusable PHP CRUD DATABASE FUNCTIONS.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel 9 Resize Image Before Upload Example
  2. Read Also:  Solution Fatal error: Call to undefined function base_url() in Codeigniter
  3. Read Also:  PHP MySQL Connect Database Script
  4. Read Also:  Ajax Image Upload without Refreshing Page using Jquery
  5. Read Also:  PHP CURL Download File example
  6. Read Also:  Simple CodeIgniter 3 login MySQL Database
  7. Read Also:  PHP OOP CRUD MVC Framework Tutorial
  8. Read Also:  No Access-Control-Allow-Origin header
  9. Read Also:  Laravel 6 Set Timestamps using Eloquent Model
  10. Read Also:  crud with php oop and mvc design pattern
CLOSEX