[Lambda-cvs] lambda/include mysql.php,1.3,1.4
Status: Pre-Alpha
Brought to you by:
ariejan
From: Ariejan de V. <ar...@us...> - 2005-07-26 14:36:05
|
Update of /cvsroot/lambda/lambda/include In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9918/include Modified Files: mysql.php Log Message: Added db_update Index: mysql.php =================================================================== RCS file: /cvsroot/lambda/lambda/include/mysql.php,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** mysql.php 26 Jul 2005 14:22:39 -0000 1.3 --- mysql.php 26 Jul 2005 14:35:25 -0000 1.4 *************** *** 173,176 **** --- 173,178 ---- if(is_array($conditions)) { + $sql .= " WHERE"; + foreach($conditions as $condition) { *************** *** 226,228 **** --- 228,277 ---- return db_query($sql); } + + /** + * db_update + * + * Easy UPDATE function + * + * table: string *Required* + * data: array with new data to be updated ('field' => 'value') *Required* + * conditions: array with conditions. Null will update ALL ROWS!! *Recommended* + * + * Returns true on succes, otherwise false + **/ + function db_update($table, $data, $conditions) + { + $sql = "UPDATE `".$table."` SET"; + + if(is_array($data)) + { + while(list($key, $val) = each($data)) + { + $sql .= " `".$key."`='".$val."',"; + } + // Remove trailing comma + $sql = substr($sql, 0, -1); + + if(is_array($conditions)) + { + $sql .= " WHERE"; + + foreach($conditions as $condition) + { + $sql .= " ". $condition ." AND"; + } + + // Remove trailing AND + $sql = substr($sql, 0, -4); + } + + // Perform query + return db_query($sql); + } + else + { + // No data + return false; + } + } ?> \ No newline at end of file |