$database = 'test';
$table = 'users';
$fields = array('username','password','age','sex');
$values = array(array('u1','p1',20,1),array('u2','p2',21),array('u3','p3',22,1));
$sql = sprintf( "INSERT INTO `%s`.`%s`(%s) VALUE ", $database, $table, implode( ',',$fields ) );
$nv= count($values);
$nf = count($fields);
for ( $i = 0; $i < $nv; $i++ )
{
$val = '';
for( $j = 0; $j < $nf; $j++ )
{
$val .= sprintf( "'%s',", $values[$i][$j] );
}
$sql .= sprintf( "(%s),", substr( $val, 0, -1 ) );
}
echo substr( $sql, 0, -1);
?>
会得到以下结果:
INSERT INTO `test`.`users`(username,password,age,sex) VALUE ('u1','p1','20','1'),('u2','p2','21','1'),('u3','p3','22','1')
阅读(996) | 评论(0) | 转发(0) |