fork download
  1. <?php
  2.  
  3. $arr = array(
  4. "id" => 1,
  5. "fname" => "john",
  6. "lname" => "doe",
  7. "dob" => "10/12/2012"
  8. );
  9.  
  10. $table = "foo";
  11.  
  12. $set = "";
  13.  
  14. // todo sanitise with mysql_escape_string()
  15. foreach($arr as $key => $v) {
  16. $val = is_numeric($v) ? $v : "'" . $v . "'";
  17.  
  18. $set .= sprintf("%s=%s%s", $key, $val, ($v == end($arr) ? "" : ", "));
  19. }
  20.  
  21. $sql = sprintf("UPDATE %s SET %s", $table, $set);
  22.  
  23. echo($sql);
  24.  
  25. ?>
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
UPDATE foo SET id=1, fname='john', lname='doe', dob='10/12/2012'