Home » Reusable PHP CRUD DATABASE FUNCTIONS

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:  WordPress Force SSL HTTPS On All Pages

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 Get HTTP_HOST Name using Laravel Example

// 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:  PHP and Ajax Asynchronous File Upload using jQuery
  2. Read Also:  Laravel Create Table using Migration and model
  3. Read Also:  10 Useful PHP Filesystem Functions with Example
  4. Read Also:  Chosen Ajax Autocomplete Textbox using jQuery, PHP
  5. Read Also:  jquery empty/null json object Example
  6. Read Also:  jQuery Multiple Class Selectors Match Examples
  7. Read Also:  VueJS nested child, template, inner template, components
  8. Read Also:  Laravel Nova Minimum Requirements
  9. Read Also:  PHP MySQL Connect Database Script
  10. Read Also:  PHP OOP CRUD MVC Framework Tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *