SQL Write operation set contents (array to String)

Source: Internet
Author: User
Tags array to string

SQL Write operation set contents (array to string) Sqlset content SQL operations array to string
    1. SQL Write operation set contents (array to string) [encapsulation Method]

  1. function getsqlset ( $data ) { 
  2. $Str = ; 
  3. foreach ( $data as $k = $v ) {
  4. $Str. = $k. "= '". $v. "',";
  5. }
  6. $Str = substr ($Str,0,-1);
  7. return $Str;
  8. }

If you are performing an insert operation (add a record to user)

  1. $data = array(
  2. ' name '=' Zhang San ',
  3. ' sex '=' man',
  4. ' Idcard '='341215645646545',
  5. ' address' = ' Huaguoshan ',
  6. );
  7. $str = Getsqlset ($data);
  8. $sql = "INSERT into user set {$STR}";//Insert ... Set method is extension method, single record insert operation ... values ... Fast Speed
  9. mysql_query ($sql);

2.SQL Insert ... values ... Array composition string method

  1. $data = array(
  2. ' name '=' Zhang San ',
  3. ' sex '=' man',
  4. ' Idcard ' = '
  5. ' address' = ' Huaguoshan ',
  6. );
  7. $insertkeys = Join ("' , ' ", Array_keys ($data));  
  8. $insertvalues =" ' " . Join (" ", ' ", Array_values ($data)). " ";  
  9. mysql_query ("INSERT into user ({$insertkeys}) VALUES ({$insertvalues})";

-Day Walk Jian, gentleman to self-improvement!

SQL Write operation set contents (array to String)

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.