note 39250 added to function.pg-insert
| From: | shane at treesandthings dot com | Date: | Thu, 22 Jan 2004 01:04:14 +0000 |
| Subject: | note 39250 added to function.pg-insert | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-63993@lists.php.net to get a copy of this message | ||
Returns SQL statement, slight improvement on the code from 'rorezende at hotmail dot com'.
This version adds bool values correctly.It also checks to make sure there is actually a value in
the array before including it in the sql statement. (ie: null values or empty strings won't be
added to the sql statement)
<?PHP
function db_build_insert($table,$array)
{
$str = "insert into $table ";
$strn = "(";
$strv = " VALUES (";
while(list($name,$value) = each($array)) {
if(is_bool($value)) {
$strn .= "$name,";
$strv .= ($value ? "true":"false") . ",";
continue;
};
if(is_string($value)) {
$strn .= "$name,";
$strv .= "'$value',";
continue;
}
if (!is_null($value) and ($value != "")) {
$strn .= "$name,";
$strv .= "$value,";
continue;
}
}
$strn[strlen($strn)-1] = ')';
$strv[strlen($strv)-1] = ')';
$str .= $strn . $strv;
return $str;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.pg-insert.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39250
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39250&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39250&report=yes
Search -- http://master.php.net/manage/user-notes.php