With the help of following code snippet, you will be able to build a long sql query for insert/update easily from an associative array, which keys are same as the column fields of database. The only requirement is that, the supplied associative array should have key names as the column name on mysql database.
function get_sql_insert_srting($table, $data)
{
$key = array_keys($data);
$val = array_values($data);
$sql = "INSERT INTO $table (" . implode(', ', $key) . ") "
. "VALUES ('" . implode("', '", $val) . "')";
return($sql);
}
function get_sql_update_string($table, $data, $where)
{
$cols = array();
foreach($data as $key=>$val)
{
$cols[] = "$key = '$val'";
}
$sql = "UPDATE $table SET " . implode(', ', $cols) . " WHERE $where";
return($sql);
}
Subscribe to:
Post Comments (Atom)










0 comments:
Post a Comment