2020年6月5日

    1. <?php
    2. echo"<pre>";
    3. include './demo6.php';
    4. include './Model.demo7.php';
    5. $obj=Model::connect();
    6. $res=$obj
    7. ->table('books')
    8. ->where('book_id','=',1000)
    9. ->sava([
    10. 'title' => 'qt'
    11. ]);
    12. var_dump("执行语句结果:",$res);
    13. var_dump($obj);
    14. echo"</pre>";
    15. class Model{
    16. private static $obj =null;//保存model对象
    17. protected $link= null;//保存数据库链接
    18. protected $tableName;//保存表名
    19. protected $sql;//保存完成的sql语句
    20. protected $where ='WHERE';//保存条件
    21. private function{
    22. try{
    23. //连接数据库
    24. $this->link=new PDO(DSN,USER,PASS);
    25. //设置错误报告
    26. $this->link->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    27. }catch(PDOException $e){
    28. exit($e->getMessage());
    29. }
    30. }
    31. public static function connect(){
    32. if(self::$obj===null){
    33. self::$obj=new self();
    34. }
    35. return self::$obj;
    36. }
    37. public static add($data){
    38. $keys=array_keys($data);
    39. $vals=array_values($data);
    40. //生成字段
    41. $keys_str=implode(',',$keys);
    42. $vals_str=sprintf("'$s'",implode("','",$vals));
    43. $this->getsql('add',[
    44. $keys_str,
    45. $vals_str,
    46. ]);
    47. return $this->exec();
    48. }
    49. //数据库删除函数
    50. public function delete{
    51. $this->getsql('del');
    52. return $this->exec();
    53. }
    54. //修改函数
    55. public function sava{
    56. $sava_data=[];
    57. foreach($data as $k => $v){
    58. $sava_data[]=sprintf("%s='$s'",$k,$v);
    59. }
    60. $sava_data_str=imlode(',',$sava_data);
    61. $this->getsql('edit',[
    62. $sava_data_str
    63. ]);
    64. return $this->exec();
    65. }
    66. //辅助函数 设置表名
    67. public function table($name){
    68. $this->tableName=$name;
    69. return $this;
    70. }
    71. protected function getsql($type,$data=[]){
    72. if(!$this->tableName){
    73. exit("请设置表名");
    74. }
    75. switch($type){
    76. case 'add':
    77. $this->sql = sprintf("INSERT INTO %s(%s) VALUES(%s)",$this->tableName,$data[0],$data[1]);
    78. break;
    79. case 'del':
    80. if($this->where == 'WHERE'){
    81. exit("delete操作不能没有where条件;");
    82. }
    83. $this->sql=sprintf("DELETE FROM %s %s",$this->tableName,$this->where);
    84. break;
    85. case 'edit':
    86. if($this->where == 'WHERE'){
    87. $this->sql=sprintf("UPDATE %s SET %s %s",$this->tableName,$data[0],$this->where);
    88. break;
    89. }
    90. }
    91. }
    92. public function exec(){
    93. return $this->link->exec($this->sql);
    94. }
    95. public function where($field,$mark,$data,$rel='AND'){
    96. if($this->where=='WHERE'){
    97. //第一个条件
    98. $this->.=sprintf("%s %s '%s'",$field,$mark,$data);
    99. }else{
    100. //第二个条件和第n个
    101. $this->.=sprintf(" %s %s %s %s",$rel,$field,$mark,$data);
    102. }
    103. return $this;
    104. }
    105. //设置查询时的字段
    106. public function field(){
    107. $p=func_get_args();
    108. $this->field-implode(',',$p);
    109. return $this;
    110. }
    111. }
    112. <?php
    113. echo"<pre>";
    114. include './demo6.php';
    115. include './Model.demo7.php';
    116. $obj=Model::connect();
    117. $res=$obj
    118. ->table('books')
    119. ->where('book_id','=',1000)
    120. ->sava([
    121. 'title' => 'qt'
    122. ]);
    123. var_dump("执行语句结果:",$res);
    124. var_dump($obj);
    125. echo"</pre>";
    126. class Model{
    127. private static $obj =null;//保存model对象
    128. protected $link= null;//保存数据库链接
    129. protected $tableName;//保存表名
    130. protected $sql;//保存完成的sql语句
    131. protected $where ='WHERE';//保存条件
    132. private function{
    133. try{
    134. //连接数据库
    135. $this->link=new PDO(DSN,USER,PASS);
    136. //设置错误报告
    137. $this->link->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    138. }catch(PDOException $e){
    139. exit($e->getMessage());
    140. }
    141. }
    142. public static function connect(){
    143. if(self::$obj===null){
    144. self::$obj=new self();
    145. }
    146. return self::$obj;
    147. }
    148. public static add($data){
    149. $keys=array_keys($data);
    150. $vals=array_values($data);
    151. //生成字段
    152. $keys_str=implode(',',$keys);
    153. $vals_str=sprintf("'$s'",implode("','",$vals));
    154. $this->getsql('add',[
    155. $keys_str,
    156. $vals_str,
    157. ]);
    158. return $this->exec();
    159. }
    160. //数据库删除函数
    161. public function delete{
    162. $this->getsql('del');
    163. return $this->exec();
    164. }
    165. //修改函数
    166. public function sava{
    167. $sava_data=[];
    168. foreach($data as $k => $v){
    169. $sava_data[]=sprintf("%s='$s'",$k,$v);
    170. }
    171. $sava_data_str=imlode(',',$sava_data);
    172. $this->getsql('edit',[
    173. $sava_data_str
    174. ]);
    175. return $this->exec();
    176. }
    177. //辅助函数 设置表名
    178. public function table($name){
    179. $this->tableName=$name;
    180. return $this;
    181. }
    182. protected function getsql($type,$data=[]){
    183. if(!$this->tableName){
    184. exit("请设置表名");
    185. }
    186. switch($type){
    187. case 'add':
    188. $this->sql = sprintf("INSERT INTO %s(%s) VALUES(%s)",$this->tableName,$data[0],$data[1]);
    189. break;
    190. case 'del':
    191. if($this->where == 'WHERE'){
    192. exit("delete操作不能没有where条件;");
    193. }
    194. $this->sql=sprintf("DELETE FROM %s %s",$this->tableName,$this->where);
    195. break;
    196. case 'edit':
    197. if($this->where == 'WHERE'){
    198. $this->sql=sprintf("UPDATE %s SET %s %s",$this->tableName,$data[0],$this->where);
    199. break;
    200. }
    201. }
    202. }
    203. public function exec(){
    204. return $this->link->exec($this->sql);
    205. }
    206. public function where($field,$mark,$data,$rel='AND'){
    207. if($this->where=='WHERE'){
    208. //第一个条件
    209. $this->.=sprintf("%s %s '%s'",$field,$mark,$data);
    210. }else{
    211. //第二个条件和第n个
    212. $this->.=sprintf(" %s %s %s %s",$rel,$field,$mark,$data);
    213. }
    214. return $this;
    215. }
    216. //设置查询时的字段
    217. public function field(){
    218. $p=func_get_args();
    219. $this->field-implode(',',$p);
    220. return $this;
    221. }
    222. }
    223. <?php
    224. echo"<pre>";
    225. include './demo6.php';
    226. include './Model.demo7.php';
    227. $obj=Model::connect();
    228. $res=$obj
    229. ->table('books')
    230. ->where('book_id','=',1000)
    231. ->sava([
    232. 'title' => 'qt'
    233. ]);
    234. var_dump("执行语句结果:",$res);
    235. var_dump($obj);
    236. echo"</pre>";
    237. class Model{
    238. private static $obj =null;//保存model对象
    239. protected $link= null;//保存数据库链接
    240. protected $tableName;//保存表名
    241. protected $sql;//保存完成的sql语句
    242. protected $where ='WHERE';//保存条件
    243. private function{
    244. try{
    245. //连接数据库
    246. $this->link=new PDO(DSN,USER,PASS);
    247. //设置错误报告
    248. $this->link->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    249. }catch(PDOException $e){
    250. exit($e->getMessage());
    251. }
    252. }
    253. public static function connect(){
    254. if(self::$obj===null){
    255. self::$obj=new self();
    256. }
    257. return self::$obj;
    258. }
    259. public static add($data){
    260. $keys=array_keys($data);
    261. $vals=array_values($data);
    262. //生成字段
    263. $keys_str=implode(',',$keys);
    264. $vals_str=sprintf("'$s'",implode("','",$vals));
    265. $this->getsql('add',[
    266. $keys_str,
    267. $vals_str,
    268. ]);
    269. return $this->exec();
    270. }
    271. //数据库删除函数
    272. public function delete{
    273. $this->getsql('del');
    274. return $this->exec();
    275. }
    276. //修改函数
    277. public function sava{
    278. $sava_data=[];
    279. foreach($data as $k => $v){
    280. $sava_data[]=sprintf("%s='$s'",$k,$v);
    281. }
    282. $sava_data_str=imlode(',',$sava_data);
    283. $this->getsql('edit',[
    284. $sava_data_str
    285. ]);
    286. return $this->exec();
    287. }
    288. //辅助函数 设置表名
    289. public function table($name){
    290. $this->tableName=$name;
    291. return $this;
    292. }
    293. protected function getsql($type,$data=[]){
    294. if(!$this->tableName){
    295. exit("请设置表名");
    296. }
    297. switch($type){
    298. case 'add':
    299. $this->sql = sprintf("INSERT INTO %s(%s) VALUES(%s)",$this->tableName,$data[0],$data[1]);
    300. break;
    301. case 'del':
    302. if($this->where == 'WHERE'){
    303. exit("delete操作不能没有where条件;");
    304. }
    305. $this->sql=sprintf("DELETE FROM %s %s",$this->tableName,$this->where);
    306. break;
    307. case 'edit':
    308. if($this->where == 'WHERE'){
    309. $this->sql=sprintf("UPDATE %s SET %s %s",$this->tableName,$data[0],$this->where);
    310. break;
    311. }
    312. }
    313. }
    314. public function exec(){
    315. return $this->link->exec($this->sql);
    316. }
    317. public function where($field,$mark,$data,$rel='AND'){
    318. if($this->where=='WHERE'){
    319. //第一个条件
    320. $this->.=sprintf("%s %s '%s'",$field,$mark,$data);
    321. }else{
    322. //第二个条件和第n个
    323. $this->.=sprintf(" %s %s %s %s",$rel,$field,$mark,$data);
    324. }
    325. return $this;
    326. }
    327. //设置查询时的字段
    328. public function field(){
    329. $p=func_get_args();
    330. $this->field-implode(',',$p);
    331. return $this;
    332. }
    333. }

    笔记:
    Model:封装数据库的增删改查操作函数

    数据库的新增操作:
    INSERT INTO money(name,rmb)VALUES(‘QT’,’1000’)
    INSERT INTO 表名(字段名,字段名2…)VALUES(值,值2…)
    数组方式:[‘name’=>’qt’]按前面的格式;设置字段和值
    数组的下标为字段名,数组的值为要添加的值

    封装生成语句函数:
    $type:add
    del
    edit
    get

    修改函数:
    UPDATA money SET 字段=值,字段=值 WHERE id=1

    封装where条件设置:
    WHERE id= 1;
    WHERE id= 1 OR id = 2;
    WHERE id= 1AND name =2;
    WHERE id= 1AND name =2 AND title = 3;
    WHERE id= 1AND name =2 OR title = 3;
    每调用一次则设置一次where条件;当第一次设置时则直接设置,当第二次或多次时那新增条件需要和原有条件相结合