SQL写操作 设置内容 (数组转字符串)
时间:2021-07-01 10:21:17
帮助过:33人阅读
getSqlSet( $data ){
$Str = "";
foreach( $data as $k => $v ){
$Str.= $k."=‘".$v."‘,";
}
$Str = substr($Str,0,-1);
return $Str;
}
如执行insert操作(给user添加一条记录)
- $data = array(
- ‘name‘=>‘张三‘,
- ‘sex‘=>‘男‘,
- ‘idcard‘=>‘341215645646545‘,
- ‘address‘=>‘花果山‘,
- );
- $str = getSqlSet($data);
- $sql = "INSERT INTO user set {$str}"; // insert ... set 方法为扩展方法,单条记录插入操作较 ...values... 速度快
- mysql_query($sql);
2.SQL insert … values… 数组组成字符串方法
- $data = array(
- ‘name‘=>‘张三‘,
- ‘sex‘=>‘男‘,
- ‘idcard‘=>‘341215645646545‘,
- ‘address‘=>‘花果山‘,
- );
- $insertkeys = join("‘,‘", array_keys($data) ) ;
- $insertvalues = "‘" . join("‘,‘", array_values($data) ) . "‘";
- mysql_query( "INSERT INTO user ({$insertkeys}) VALUES ({$insertvalues})" );
— 天行健,君子以自强不息!
SQL写操作 设置内容 (数组转字符串)
标签:value optimize name bsp 数组 rem ros ase lines