Project

General

Profile

« Previous | Next » 

Revision 1364

Added by Dietmar almost 14 years ago

added function 'db_update_key_value()'

View differences:

class.database.php
315 315
	}
316 316

  
317 317
}
318

  
318
/* this function is placed inside this file temporarely until a better place is found */
319
/*  function to update a var/value-pair(s) in table ****************************
320
 *  nonexisting keys are inserted
321
 *  @param string $table: name of table to use (without prefix)
322
 *  @param mixed $key:    a array of key->value pairs to update
323
 *                        or a string with name of the key to update
324
 *  @param string $value: a sting with needed value, if $key is a string too
325
 *  @return bool:  true if any keys are updated, otherwise false
326
 */
327
	function db_update_key_value($table, $key, $value = '')
328
	{
329
		global $database;
330
		if( !is_array($key))
331
		{
332
			if( trim($key) != '' )
333
			{
334
				$key = array( trim($key) => trim($value) );
335
			} else {
336
				$key = array();
337
			}
338
		}
339
		$retval = true;
340
		foreach( $key as $index=>$val)
341
		{
342
			$index = strtolower($index);
343
			$sql = 'SELECT COUNT(`setting_id`) FROM `'.TABLE_PREFIX.$table.'` WHERE `name` = \''.$index.'\' ';
344
			if($database->get_one($sql))
345
			{
346
				$sql = 'UPDATE ';
347
				$sql_where = 'WHERE `name` = \''.$index.'\'';
348
			}else {
349
				$sql = 'INSERT INTO ';
350
				$sql_where = '';
351
			}
352
			$sql .= '`'.TABLE_PREFIX.$table.'` ';
353
			$sql .= 'SET `name` = \''.$index.'\', ';
354
			$sql .= '`value` = \''.$val.'\' '.$sql_where;
355
			if( !$database->query($sql) )
356
			{
357
				$retval = false;
358
			}
359
		}
360
		return $retval;
361
	}
319 362
?>

Also available in: Unified diff