Create Dynamic SQL Update Query in PHP and MySql

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 dynamic update query.

Simple MySQL Update Query Example

Create build_sql_update() a php method into the php file or application.

if table has 2 column, one is name and another column name is age. Then data array should be array(‘name’ => ‘parvez’, ‘age’ => ’26’).

We will define method as follows.

function build_sql_update($table, $data, $where)

The function takes three argument:

  1. The first one is the table name where it will insert
  2. $data what will insert. The $data is a key value pair of array.
  3. Third one is the 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,

Source Code to create dynamic update Query using php and MySQL

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.