What is the best way to construct an UPDATE statement using associative arrays in PHP?
For example, say I have a function like so:
/**
* For update queries
*
* @param string $tableName Name of the table we're wanting to update.
* @param array $values Associative array of columns / values to update. e.g. array('name' => 'John', 'age' => 29)
* @param array $conditions Associative array of conditions. e.g. array('user_id' => 1) equates to "WHERE user_id = 1"
*/
public function update($tableName, $values, $conditions = array()){
//Construct SQL
}
So far, I've been able to construct simple UPDATE statements such as:
UPDATE `myTableName` SET `name` = :name, `age` = :age WHERE `user_id` = :user_id
Now I'm left wondering: What is the best way to approach constructing the WHERE clause? Are there similar implementations in other libraries and code bases that I can look into? For example: How do I approach the construction of WHERE clauses that have OR and AND and IN() etc?
UPDATE example SET col = :val WHERE user_id = :user_id AND (age = :age OR name = :name)