Created
August 10, 2011 06:09
-
-
Save yuksbg/1136233 to your computer and use it in GitHub Desktop.
php_array_to_sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function arrayToSql($table, $array, $insert = "INSERT INTO") { | |
if ($insert != "UPDATE") { | |
$insert = "INSERT INTO"; | |
} | |
$columns = array (); | |
$data = array (); | |
foreach ( $array as $key => $value ) { | |
$columns [] = $key; | |
if ($value != "") { | |
$data [] = "'" . $value . "'"; | |
} else { | |
$data [] = "NULL"; | |
} | |
} | |
$cols = implode ( ",", $columns ); | |
$values = implode ( ",", $data ); | |
$sql = <<<EOSQL | |
$insert `$table` | |
($cols) | |
VALUES | |
($values) | |
EOSQL; | |
return $sql; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment