PHP implementation Text data import SQL server_php tutorial

Source: Internet
Author: User

PHP implements text data import into SQL SERVER


PHP to import text files to MySQL we often encounter, but if it is imported to SQL Server and how to operate it, the following will give you to share my operation method, the sense of efficiency is also good, recommended to everyone here.

My test is supported by Csv,sql,txt

In fact, other formats are possible, but I can't use them.

The speed is almost 10,000 per second.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

Set_time_limit (0);

Ignore_user_abort (TRUE);

Include (' sgk.php ');

echo "

";

?>

function Array_iconv ($data, $output = ' GBK ') {

$encode _arr = Array (' UTF-8 ', ' ASCII ', ' GBK ', ' GB2312 ', ' BIG5 ', ' JIS ', ' Eucjp-win ', ' Sjis-win ', ' euc-jp ');

$encoded = mb_detect_encoding ($data, $encode _arr);//Automatic judgment code

if (!is_array ($data)) {

Return mb_convert_encoding ($data, $output, $encoded);

}

else {

foreach ($data as $key = = $val) {

if (Is_array ($val)) {

$data [$key] = Array_iconv ($val, $input, $output);

} else {

$data [$key] = mb_convert_encoding ($data, $output, $encoded);

}

}

return $data;

}

}

function Microtime_float () {

Return Microtime (TRUE);

}

if ($_session[' id ']<> ') {

if (@$_post["Submit"]<> "") {

if (($_files["File" ["type"] = = "Text/plain")

|| ($_files["File" ["type"] = = "Application/vnd.ms-excel") | | ($_files["File" ["type"] = = "Application/octet-stream")

|| ($_files["File" ["type"] = = "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))

&& ($_files["File" ["Size"] < 198102805))

{

if ($_files["file" ["error"] > 0)

{

echo "Return Code:". $_files["File" ["Error"]. "
";

}

Else

{

$name =$_files["File" ["Name"];

$name =iconv (' utf-8 ', ' Gb2312//ignore ', $_files[' file ' [' name ']);

echo "Upload:". $_files["File" ["Name"]. "
";

echo "Size:". ($_files["File" ["Size"]/1048576). "Mb
";

$exit = PathInfo ($_files["File" ["Name"], pathinfo_extension);

$exit = '. '. $exit;

if ($exit = = ". asp" or $exit = = ". php" or $exit = = ". jsp" or $exit = = ". aspx") {

echo "I rub, what do you want to do?" ";

Exit

}

$size =filesize ($_files["File" ["Tmp_name"]);

$_files["File" ["Name"]=md5 ($_files["file"] ["name"]. $size). $exit;

if (file_exists ("uploadhehe/". $_files["File" ["Name"]))

{

echo $_files["File" ["Name"]. "Already exists";

}

Else

{

Move_uploaded_file ($_files["file"] ["Tmp_name"],

"uploadhehe/". $_files["File" ["name"]);

echo "
Start Automatic processing of files
";

$begin = Microtime_float ();

$file = "uploadhehe/". $_files["File" ["Name"];

$handle = @fopen ($file, "R");

$handle =array_iconv (' utf-8 ', ' GBK ', $handle);

$total = 0;

$over = 0;

if ($handle) {

$sql = "INSERT into [dbo]. [Own_user_data1] ([Name],[context],[md5],[data]) VALUES ";

$num = 0;

$_files[' file ' [' Name ']=iconv (' utf-8 ', ' Gb2312//ignore ', $_files[' file ' [' name ']);

while (!feof ($handle)) {

$buffer = Fgets ($handle, 2000);

$buffer =str_replace ("\ n", "", $buffer);

$line _array=explode ("\ r \ n", $buffer);

$Context =htmlspecialchars (addslashes ($line _array[0));

$Context =str_replace ("'", ', ', $Context);

$Context =str_replace (' "', ', ', $Context);

$Context =array_iconv ($Context);

$date =date (' y-m-d h:i:s ', Time ());

if ($num <999) {

$sql. = "(' {$name} ', ' {$Context} ', ' {$_files[' file ' [' Name ']} ', ' {$date} '),";

}else{

$sql. = "(' {$name} ', ' {$Context} ', ' {$_files[' file ' [' Name ']} ', ' {$date} ')";

$a =sqlsrv_query ($conn, $sql);

if ($a = = = False) {

Die (Print_r (Sqlsrv_errors (), true));

// }

$num = 0;

$sql = "INSERT into [dbo]. [Own_user_data1] ([Name],[context],[md5],[data]) VALUES ";

}

$num + +;

$total + +;

}

}

Fclose ($handle);

$sql 1= "INSERT into [dbo]. [Own_user_info] ([Uid],[md5],[date],[name],[size],[gold]) VALUES (' {$_session[' id '} ', ' {$_files[' file ' [' Name ']} ', ' {$date} ', ' {$name} ', ' {$total} ', ' 1 ');

Sqlsrv_query ($conn, $sql 1);

if ($a = = = False) {

Die (Print_r (Sqlsrv_errors (), true));

}

Processing records that are not inserted into the database

if (substr ($sql, strlen ($sql) -1,1) = = ",") {

$sql =substr ($sql, 0,strlen ($sql)-1);

Sqlsrv_query ($conn, $sql);

}

$end = Microtime_float ();

$time = $end-$begin;

echo "Total import {$total} record, time {$time} seconds";

}

}

}

Else

{

echo "Upload failed! ";

}

}

}else{

Header ("Location:login.php");

}

?>

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1000067.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000067.html techarticle PHP Implementation of text data import SQL SERVER php to import text files to MySQL we often encounter, but if it is imported to SQL Server and how to do it, the following will give you to share my ...

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