Php uses arrays to generate SQL statements to execute. In php, arrays are used to generate SQL statements to be executed. will array SQL encounter such a situation? Each time you obtain data, the data and historical versions are different. However, in php, arrays are used to generate SQL statements to be executed.
Will this happen? The data obtained each time is different from the previous version. However, the addAll () function of ThinkPHP deletes the existing data and writes it again. This is obviously not what we want. However, I am also drunk when I write SQL statements on dozens of fields each time. How can we achieve SQL automatic generation elegantly and easily? So we have the following method.
/*** [Array_to_ SQL concatenates the array key and value into the required SQL] * @ param [type] $ array [key, value structure array] * @ param string $ type [SQL type insert, update] * @ param array $ exclude [excluded fields] * @ return [string] [return spliced SQL] */function array_to_ SQL ($ array, $ type = 'insert ', $ exclude = array () {$ SQL = ''; if (count ($ array)> 0) {foreach ($ exclude as $ exkey) {unset ($ array [$ exkey]); // remove unnecessary keys} if ('insert' = $ type) {$ keys = array_keys ($ array ); $ values = array_values ($ array); $ col = implode ("','", $ keys); $ val = implode ("','", $ values ); $ SQL = "('$ col') values (' $ val ')";} else if ('update' = $ type) {$ tempsql = ''; $ temparr = array (); foreach ($ array as $ key => $ value) {$ tempsql = "'$ key' =' $ value '"; $ temparr [] = $ tempsql;} $ SQL = implode (",", $ temparr) ;}} return $ SQL ;}
Of course, this method has many points to consider. Usable, but not the best. Do you have a better method for Yuanshu? Please let me know!
Will the hacker encounter such a situation? Each time you obtain data, the data is different from the previous version...