The implementation of the SQL statement to be executed by the array in PHP, the array sql_php tutorial

Source: Internet
Author: User

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.