For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > June 2006 > Re: [PHP-DB] Conditional updating...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] Conditional updating...
J R

2006-06-28, 3:57 am

try this one.

just pass for $_value an array with your table fieldname as your key

function constructUpdate( $_tbl_name, $_where, $_values )
{
$valstr = '';
$firstval = false;
if (is_array($_values)) {
foreach( $_values as $key=>$val ) {
if ($val != '') {
if( $firstval )
$valstr.= ',';

if (is_string($val)) {
$valstr.= " $key = '$val'";
} else {
$valstr.= " $key = $val";
}
$firstval = true;
}
}
} else {
$valstr = $_values;
}
$retStr = "update $_tbl_name set $valstr";
if( $_where )
$retStr.= " where $_where";

return $retStr;
}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com