<?php //数据库连接; function con(){ $dsn='mysql: host=127.0.0.1; dbname=luheng; charset=utf8; port=3306'; $dbname ='root'; $dbpw ='root'; try{ $p = new PDO($dsn,$dbname,$dbpw); }catch(PDOException $e){ print_r($e->grtMessige()); exit; } return $p; } //普通的查询; function select(){ //连接数据库; $c=con(); //编写sql语句; $sql='select * from demo'; //预处理sql语句; $a = $c->prepare($sql); //执行sql语句; $a->execute(); //改变类型; $a->setFetchMode(PDO::FETCH_ASSOC); //结果集; $ret = $a->fetchAll(); //返回结果集; return $ret; } // $b=select(); // print_r($b); function select($table,$field='*',$where=''){ //首先连接数据库; $c=con(); $sql = 'SELECT '; // 判断字段不为空; if(!empty($field)){ $sql .= rtrim($field); } $sql .= ' FROM '; $sql .= $table; //判断条件不为空; if (!empty($where)) { $sql .= ' where '; $sql .= $where; } //预处理sql语句; $a = $c->prepare($sql); //判断是否执行成功; if ($a->execute()) { //在判断是否有返回的结果,返回记录条数; if ($a->rowCount()) { $a->setFetchMode(PDO::FETCH_ASSOC); $ret = $a->fetchAll(); return $ret; } else{ return false; } }else { return false; } } //条件不传值就是默认为所有;表名必须传; $b = select('demo'); print_r($b); //组基本的添加,不用;正常添加 function add(){ //连接数据库; $c=con(); //声明sql语句; 一定要用双引号; $sql="insert into demo (name,age,sex) VALUES ('王五',12,1)"; print_r($sql);die; //进行预处理; $a=$c->prepare($sql); //执行; //判断执行是否成功; $a->execute(); //判断是否有返回的结果集; //改变类型; $a->setFetchMode(PDO::FETCH_ASSOC); //进行结果输出; $ret = $a->fetchAll(); return $ret; } // $b = add(); // print_r($b); //添加成功; // insert into demo set ('name'=>'张三') 添加; function insert($table,$data=[]){ $pdo = con(); //创建SQL语句 $sql = "INSERT INTO {$table} SET "; // print_r($data);exit();有返回的值; //组装插入语句 if(is_array($data)){ foreach ($data as $k=>$v) { $sql .= $k.'="'.$v.'", '; //拼装为 $k ='$v', } }else{ echo "不是数组;"; return false; } // print_r($data);exit(); //去掉尾部逗号,并添加分号结束 $sql = rtrim(trim($sql),',').';'; // print_r($sql);die; //创建PDO预处理对象 $stmt = $pdo->prepare($sql); //执行新增操作 if($stmt->execute()){ if($stmt->rowCount()){ echo "添加成功"; return true; } } else { echo '添加失败;'; return false; } } // 用中括号就代表是数组,用小括号就需要加array申明一下这是数组; // $b=insert('demo',['name'=>'鲁先生','age'=>18,'age'=>0]); // $b=insert('demo',array('name'=>'鲁先生','age'=>18,'age'=>0)); // print_r($b); //正常添加; //insert into demo() values(); function add($table,$field,$values){ $pdo = con(); //开始拼接sql语句; $sql = "INSERT INTO {$table}"; $sql .='(' .$field. ')' ; // print_r($sql);die; $str = ''; foreach ($values as $v) { $str .= '"'.$v.'",'; } //去掉尾部逗号 $s = rtrim($str,','); $sql .= ' values ('.$s.')'.';'; //创建PDO预处理对象 $stmt = $pdo->prepare($sql); //执行新增操作 if($stmt->execute()){ if($stmt->rowCount()){ echo "添加数据成功"; return true; } } else { echo '添加数据失败;'; return false; } } // $b=add('demo','name,age,sex',['张三',12,0]); // print_r($b); //修改正常; // update demo name='lisi' where id=$id; function update($table,$data=[],$where=''){ //连接数据库 $c=con(); $sql = "update {$table} set "; if(is_array($data)){ foreach ($data as $k=>$v) { $sql .= $k.'="'.$v.'", '; } } $sql = rtrim(trim($sql),','); // print_r($sql);die(); //查询条件 if(!empty($where)){ $sql .= ' WHERE '.$where; } $stmt = $c->prepare($sql); //执行新增操作 if($stmt->execute()){ if($stmt->rowCount()){ echo "修改数据成功"; return true; } } else { echo '修改数据失败;'; return false; } } // $b=update('demo',['name'=>'鲁先生'],'id=3'); // print_r($b); //删除成功; // delete from demo where id=$id function delete($table,$where=''){ $c=con(); $sql = "delete from {$table} " ; if (!empty($where)) { $sql .= 'where ' .$where; } // print_r($sql);die; $stmt = $c->prepare($sql); // //执行新增操作 if($stmt->execute()){ if($stmt->rowCount()){ echo "ok"; return true; } } else { echo 'no'; return false; } } // $b=delete('demo','id=40'); // print_r($b);
总结: