Implemented in PHP with an array to generate the SQL statement to execute, array sql
Is it possible to encounter such a situation? Each acquisition of data will have a certain difference between the data and the historical version, but with the thinkphp AddAll () function, the existing data will be deleted and then re-written. This is obviously not what we want. But write your own SQL every dozens of fields are also drunk. How to automate SQL generation gracefully and easily? So here's the method.
/** * [array_to_sql stitching into required SQL based on array key and value] * @param [type] $array [key, array of value structure] * @param stri ng $type [SQL type insert,update] * @param array $exclude [excluded fields] * @return [string] [return stitched SQL] */function array _to_sql ($array, $type = ' Insert ', $exclude = Array ()) {$sql = '; if (count ($array) > 0) {foreach ($exclude as $exkey) {unset ($array [$exkey]);//Reject Key} if (' Inse RT ' = = $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; }
There is, of course, much to consider in this approach. Can be used, but not the best. The apes of the garden, do you have a better way? Please tell me!
http://www.bkjia.com/PHPjc/1030143.html www.bkjia.com true http://www.bkjia.com/PHPjc/1030143.html techarticle The implementation of the SQL statement in PHP to be executed with an array, will the array SQL encounter such a situation? Each time you get the data and the historical version have a certain difference, but with ...