自己封装的Mysql简易操作类,已塞在Ben框架中,基于PDO来写的,代码风格上有些无厘头。。。
mysql.class.php
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
|
<?php class mysql extends PDO{ public $server ; public $database ; public $user ; public $password ; public $sql ; public function __construct( $server , $database , $user , $password , $port =3306){ $this ->server = $server ; $this ->database = $database ; $this ->user = $user ; $this ->password = $password ; parent::__construct( "mysql:host=$server;port=$port;dbname=$database" , $user , $password ); $this ->query( 'SET NAMES utf8' ); } public function drop( $table ){ $sql = 'DROP TABLE ' . $table . ';' ; $re = $this ->query( $sql ); if ( $re ){ return true; } else { return false; } } public function insert( $table , $name , $value =null){ $sql = "INSERT INTO " . $table . '(' ; if ( $value == null){ $arrname = array_keys ( $name ); $arrvalue = array_values ( $name ); } else { $arrname = explode ( '|' , $name ); $arrvalue = explode ( '|' , $value ); } for ( $i =0; $i < count ( $arrname ); $i ++){ if ( $i == count ( $arrname )-1){ $sql = $sql . $arrname [ $i ]; } else { $sql = $sql . $arrname [ $i ]. "," ; } } $sql = $sql . ")VALUES(" ; for ( $i =0; $i < count ( $arrvalue ); $i ++){ if ( $i == count ( $arrvalue )-1){ $sql = $sql . "'" . $arrvalue [ $i ]. "'" ; } else { $sql = $sql . "'" . $arrvalue [ $i ]. "'," ; } } $sql .= ");" ; $re = $this ->query( $sql ); if ( $re ){ return true; } else { return false; } } public function delete ( $table , $Conditionsname , $Conditionsvalue =null){ if ( $Conditionsvalue !=null){ $sql = "DELETE FROM " . $table . " WHERE " . $Conditionsname . "='" . $Conditionsvalue . "';" ; } else { $sql = "DELETE FROM " . $table . " WHERE " ; $arrname = array_keys ( $Conditionsname ); $arrvalue = array_values ( $Conditionsname ); for ( $i =0; $i < count ( $arrname ); $i ++){ if ( $i == count ( $arrname )-1){ $sql .= $arrname [ $i ]. '=' . "'" . $arrvalue [ $i ]. "'" ; } else { $sql .= $arrname [ $i ]. '=' . "'" . $arrvalue [ $i ]. "'," ; } } $sql .= ';' ; } $re = $this ->query( $sql ); if ( $re ){ return true; } else { return false; } } public function select( $table , $name , $Conditionsname , $Conditionsvalue =null){ if ( $Conditionsvalue !=null){ $sql = "SELECT " . $name . " FROM " . $table . " WHERE " . $Conditionsname . "='" . $Conditionsvalue . "';" ; } else { $sql = "SELECT " . $name . " FROM " . $table . " WHERE " ; $arrname = array_keys ( $Conditionsname ); $arrvalue = array_values ( $Conditionsname ); for ( $i =0; $i < count ( $arrname ); $i ++){ if ( $i == count ( $arrname )-1){ $sql .= $arrname [ $i ]. '=' . "'" . $arrvalue [ $i ]. "'" ; } else { $sql .= $arrname [ $i ]. '=' . "'" . $arrvalue [ $i ]. "' and " ; } } $sql .= ';' ; } $re = $this ->query( $sql ); $row = $re ->fetch(); return $row [ $name ]; } public function update( $table , $name , $value , $Conditionsname , $Conditionsvalue =null){ if ( $Conditionsvalue !=null){ $sql = "UPDATE " . $table . " SET " . $name . "= '" . $value . "' WHERE " . $Conditionsname . "='" . $Conditionsvalue . "';" ; } else { $sql = "UPDATE " . $table . " SET " . $name . "= '" . $value . "' WHERE " ; $arrname = array_keys ( $Conditionsname ); $arrvalue = array_values ( $Conditionsname ); for ( $i =0; $i < count ( $arrname ); $i ++){ if ( $i == count ( $arrname )-1){ $sql .= $arrname [ $i ]. '=' . "'" . $arrvalue [ $i ]. "'" ; } else { $sql .= $arrname [ $i ]. '=' . "'" . $arrvalue [ $i ]. "' and " ; } } $sql .= ';' ; } $re = $this ->query( $sql ); if ( $re ){ return true; } else { return false; } } public function group( $table , $name ){ $sql = "SELECT " . $name . " FROM " . $table . ";" ; $return = array (); $re = $this ->query( $sql ); while ( $row = $re ->fetch(PDO::FETCH_ASSOC)){ array_push ( $return , $row [ $name ]); } return $return ; } public function fetchall( $sql ){ $return = array (); $re = $this ->query( $sql ); while ( $row = $re ->fetch(PDO::FETCH_ASSOC)){ array_push ( $return , $row ); } return $return ; } } |