I was going through this article today.
http://vinothbabu.com/2010/05/08/update-and-insert-differences-in-syntax-is-an-inconvenience/
I was not able to understand this part of the code written by the author.
list($sets,$cols,$values)=escape_arr($sets);
$insert_sql=”INSERT INTO `avatars` “.implode(’,',$cols).”
VALUES(”.implode(’,',$values).”)”;
$update_sql=”UPDATE `avatars` SET “.implode(’,',$sets).”
WHERE userid=$userid LIMIT 1″;
and finally the conclusion part of the article.
The PHP implode turns the array of column names into a comma separated string. It does the same for the values in the INSERT statement.
$sets starts off as an associative array of column name / value pairs. This statement:
reassigns the $sets variable to be a regular array containing strings like “column_name = ‘value'”. It does this with the helper function escape_arr in the article, which returns 3 arrays. Check the documentation for list if you’re unsure what it does.
Then it uses the
implodefunction again to build one big comma separated string of the$setsarray. So effectively, it builds both INSERT and UPDATE statements given an associative array containing column names as keys, and their values as well… values.Was that the question? You can insert some var_dump statements in the code to follow what it’s doing at every step.
Edit: sorry for the messy explanation, but I gotta run for now 🙂