PHP 8.5.0 Beta 1 available for testing

Voting

: five plus four?
(Example: nine)

The Note You're Voting On

shane at treesandthings dot com
21 years ago
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;

}
?>

<< Back to user notes page

To Top