Reusable PHP CRUD DATABASE FUNCTIONS

Laravel Redirect To Another URL / Web Page - HTTP Laravel Redirects

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.

Read Also:  Windows Send Email using Command Line

PHP Reuse a MySQL object in function Examples

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:  How to get only date from datetime in CodeIgniter? - how to get current date in codeigniter?

// 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 MySQL Connect Database Script

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.

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

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