Correction status:Uncorrected
Teacher's comments:
<?php function con(){ $dsn = 'mysql:host=127.0.0.1;dbname=blog;charset=utf8;port=3306'; $dbname = 'root'; $dbpw = 'root'; $p = new PDO($dsn,$dbname,$dbpw); return $p; } function insert($table,$column,$values){ $c=con(); $sql=''; // $sql.="INSERT INTO `user` (`name`) values ('雷国恩')"; $sql.='INSERT INTO '.$table.' ('.$column.') VALUES ('.$values.')'; $a=$c->prepare($sql); $a->execute(); } function update($table,$column,$values,$where){ $c=con(); $sql=''; $sql.='UPDATE '.$table.' SET '.$column.' = '.$values.' WHERE '.$where; $a=$c->prepare($sql); $a->execute(); } function delete($table,$where){ $c=con(); $sql=''; $sql.='DELETE FROM'.$table.' WHERE '.$where; $a=$c->prepare($sql); $a->execute(); } function select($table,$field,$where='',$order='',$limit=''){ $c=con(); // $sql='SELECT * FROM `user` where `age` > 40'; // $a=$c->prepare($sql); // $a->execute(); // $a->setFetchMode(PDO::FETCH_ASSOC); // $ret=$a->fetchALL(); // return $ret; $sql='SELECT '; if(empty($field)){ $field='*'; } $sql.= $field ; $sql.=' FROM '; $sql.= $table; if(!empty($where)){ $sql.=' WHERE '; $sql.=$where; } if(!empty($order)){ $sql.=' ORDER BY '; $sql.=$order; } if(!empty($limit)){ $sql.=' LIMIT '; $sql.=$limit; } $a=$c->prepare($sql); $a->execute(); $a->setFetchMode(PDO::FETCH_ASSOC); $ret=$a->fetchALL(); return $ret; } // 查询 // $s=select('`user`','','`age`>40','`age` desc','2'); // print_r($s); // print_r(select()); // 添加 // $b=insert('`user`','`name` ,`sex`,`age`,`city`','"雷国恩","男",30,"重庆"'); // print_r($b); //修改 // update('`user`','`name`',"'保来'",'`id` = 32'); //删除 // delete('`user`','`id` = 17');
点击 "运行实例" 按钮查看在线实例