Previously, I have described how to Create Dynamic Insert SQL script Using PHP. Let’s create a dynamic update SQL query based on data and table name.
We’ll create a method to update a row data into the MySQL table, You can use this method into the foreach
method to create a dynamic update query.
Create build_sql_update()
a php method into the PHP file or application.
if the table has 2 columns, one is the name and another column is age,Then data array should be as like below:
array('name' => 'parvez', 'age' => '26')
We will define a method as follows :
function build_sql_update($table, $data, $where)
The function takes three arguments:
Where
condition and so first we will separate key and value from data array. Now we will implode key and values of arrays with where condition and create a string of update sql.Checkout other dynamic MySQL query tutorials,
/* function to build SQL UPDATE string */function build_sql_update($table, $data, $where) { $cols = array(); foreach($data as $key=>$val) { $cols[] = "$key = '$val'"; } $sql = "UPDATE $table SET " . implode(', ', $cols) . " WHERE $where"; return($sql); }
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More