Php simple operation mysql database class _ PHP Tutorial

Source: Internet
Author: User
Php is a simple mysql database class. Php simple mysql database operations are as follows: 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 php simple mysql database operations

The details are as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

/**

* Database class

*

* @ Version: 2.2

* @ Revised: 27 may 2007

*

**/

Class Database {

Var $ host;

Var $ name;

Var $ user;

Var $ pass;

Var $ prefix;

Var $ linkId;

Function Database ($ mysql ){

Foreach ($ mysql as $ k => $ v ){

$ This-> $ k = $ v;

}

If (strlen ($ this-> prefix)> 0 & substr ($ this-> prefix,-1 )! = "_")

$ Prefix. = "_";

$ This-> prefix = $ prefix;

}

Function getLastID (){

$ Id = mysql_fetch_row (mysql_query ("SELECT LAST_INSERT_ID ()", $ this-> linkId ));

Return $ id [0];

}

Function getPossibleValues ($ tableA, $ whereA ){

If (gettype ($ tableA) = "array "){

$ Table = "";

Foreach ($ tableA as $ t ){

$ Table. = $ this-> prefix. $ t .",";

}

$ Table = substr ($ table, 0,-2 );

} Else $ table = $ this-> prefix. $ tableA;

If (gettype ($ whereA )! = "Array "){

$ WhereA = array ($ whereA );

}

$ Return = array ();

Foreach ($ whereA as $ where ){

$ SQL = mysql_query ("SHOW COLUMNS FROM". $ table. "LIKE '%". $ where. "% '");

While ($ arr = mysql_fetch_array ($ SQL )){

If (strpos ($ arr ['type'], 'enum') === 0 ){

$ Vals = substr ($ arr ['type'], 5,-1 );

} Else {

$ Vals = substr ($ arr ['type'], 4,-1 );

}

$ Vals = str_replace ("'", "", $ vals );

$ Vals = explode (",", $ vals );

$ I = 1;

Foreach ($ vals as $ val ){

$ Return [$ arr ['field'] [$ I ++] = $ val;

}

$ Return [$ arr ['field'] ['default'] = $ arr ['default'];

If ($ arr ['null']! = "NO") $ return [$ arr ['field'] [0] = NULL;

}

}

Return $ return;

}

Function connect (){

$ This-> linkId = mysql_connect ($ this-> host, $ this-> user, $ this-> pass );

If (! $ This-> linkId ){

Return false;

}

If (mysql_select_db ($ this-> name, $ this-> linkId) return true;

Mysql_close ($ this-> linkId );

Return false;

}

Function runSelect ($ tables, $ where = "1", $ fieldsA = "*", $ order = false, $ limit = false, $ offset = false, $ group = false) {

If (gettype ($ tables) = "array "){

$ Table = "";

Foreach ($ tables as $ t ){

$ Table. = $ this-> prefix. $ t .",";

}

$ Table = substr ($ table, 0,-2 );

} Else $ table = $ this-> prefix. $ tables;

If (gettype ($ fieldsA) = "array "){

$ Fields = "";

$ Keys = array_keys ($ fieldsA );

If ($ keys [0]! = '0 '){

Foreach ($ keys as $ key ){

$ Fields. = $ key. 'as'. $ fieldsA [$ key]. ',';

}

} Else {

Foreach ($ fieldsA as $ field ){

$ Fields. = $ field .',';

}

}

$ Fields = substr ($ fields, 0,-2 );

} Else $ fields = $ fieldsA;

$ Query = "SELECT". $ fields. "FROM". $ table. "WHERE". $ where.

($ Order! = False? "Order by". $ order :( $ group! = False? "Group by". $ group :"")).

($ Limit! = False? "LIMIT". $ limit :"").

($ Offset! = False? "OFFSET". $ offset :"");

Return mysql_query ($ query, $ this-> linkId );

}

Function runUpdate ($ table, $ valuesA, $ where = "1 "){

If (gettype ($ valuesA) = "array "){

$ Fields = "";

$ Values = "";

$ Keys = array_keys ($ valuesA );

Foreach ($ keys as $ key ){

If ($ valuesA [$ key]! = NULL)

$ Values. = ""'. $ key. "'= '". str_replace ("'",' \ '', $ valuesA [$ key]). "',";

Else

$ Values. = $ key. "= NULL ,";

}

$ Fields = substr ($ fields, 0,-1 );

$ Values = substr ($ values, 0,-1 );

} Else $ values = $ valuesA;

$ Query = "UPDATE". $ this-> prefix. $ table. "SET". $ values. "WHERE". $ where;

If (mysql_query ($ query,

$ This-> linkId ))

Return mysql_affected_rows ($ this-> linkId );

Return false;

}

Function runDelete ($ table, $ where = "1 "){

If (mysql_query ("delete from". $ this-> prefix. $ table. "WHERE". $ where, $ this-> linkId ))

Return mysql_affected_rows ($ this-> linkId );

Return false;

}

Function runInsert ($ table, $ valuesA, $ onDuplicate = NULL ){

If (gettype ($ valuesA) = "array "){

$ Fields = "";

$ Values = "";

$ Keys = array_keys ($ valuesA );

Foreach ($ keys as $ key ){

$ Fields. = "" '. $ key ."',";

$ Values. = ($ valuesA [$ key] === NULL? "NULL,": "'". str_replace ("'", '\ '', $ valuesA [$ key])."', ");

}

$ Fields = substr ($ fields, 0,-2 );

$ Values = substr ($ values, 0,-2 );

}

$ OnDup = "";

If ($ onDuplicate! = NULL ){

$ OnDup = "on duplicate key update ";

If (gettype ($ onDuplicate) = "array "){

$ Keys = array_keys ($ onDuplicate );

Foreach ($ keys as $ key ){

$ OnDup. = '''. $ key. ''= '. ($ onDuplicate [$ key] = NULL? "NULL,": "'". str_replace ("'", '\ '', $ onDuplicate [$ key])."', ");

}

$ OnDup = substr ($ onDup, 0,-2 );

} Else $ onDup. = $ onDuplicate;

}

$ Query = "insert into". $ this-> prefix. $ table. ($ fields! = NULL? "(". $ Fields .")":"").

"VALUES (". $ values. ")". $ onDup;

If (mysql_query ($ query, $ this-> linkId ))

Return mysql_affected_rows ($ this-> linkId );

Return false;

}

Function getCells ($ table ){

$ Query = "show columns from" '. $ table .""';

$ Fields = mysql_query ($ query, $ this-> linkId) or die ('hej ');

Return $ fields;

}

Function translateCellName ($ cellName ){

$ SQL = $ this-> runSelect ("mysql_cell_translation", "mysql_name = '". $ cellName ."'");

$ Row = mysql_fetch_assoc ($ SQL );

Return $ row ['human _ name']? $ Row ['human _ name']: '['. $ cellName. ']';

}

Function getError (){

Return mysql_error ($ this-> linkId );

}

Function close ()

{

Mysql_close ($ this-> linkId );

}

}

?>

The specific region is as follows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35 36 38 39 40 41 42 43 44 45 46...

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.