Although excellent deceze's answer covers your question literally, there is something else to mention.
If you take a look at any major freamework, you will find a function... update() which works pretty the same way:
function update($table, $data, $id) {
so, the approach is quite popular in general.
Nevertheless, it's wrong and misguided as well.
Being initially inflexible, it quickly become unusable:
- once you need a field name other than conventional id,
- once you need a more complex condition,
- once you need a JOIN-based update,
- once you need to use an SQL function(!) to process some data
all these cases will make initially small & neat function into bloated monster.
But the remedy is simple.
What you really need is a placeholder for the every data type that may be added to the query. So, welcome to SafeMysql - a library that will fulfill your dreams:
$db->query("UPDATE ?n SET ?u WHERE id=?i", $table, $data, $id);
it is doing exactly what you want, but doing it proper way. And even such a complex query won't make it any trouble:
$data = array('field'=>$value,'field2'=>$value);
$ids = array(1,2,4);
$sql = "UPDATE t SET ts=unix_timestamp(), ip=inet_aton(?s),?u WHERE id IN(?a)";
$db->query($sql, $ip, $data, $ids);
Though it's purpose not to make "less [SQL] statements", but to write less PHP code and make your query safe, while keeping it familiar conventional SQL, without trade-offs like learning new language of some Query Builder which is merely an SQL dialect written in PHP.