插入语句预处理语句函数及其调用

Posted on 2015-06-05 01:19  小蕊同学  阅读(215)  评论(0编辑  收藏  举报
插入数据预处理语句函数
  1. function save($tn,$data=array()){
  2. global $m;
  3. $ty = '';
  4. foreach($data as $k=>$v){
  5. $kk[] = $k;
  6. $vv[] = '?';
  7. $dd[] = "\$data['$k']";
  8. switch(gettype($v)){
  9. case 'integer':$ty.='i';break;
  10. case 'string':$ty.='s'; break;
  11. case 'double':$ty.='d';break;
  12. default:$ty.='b';break;
  13. }
  14. }
  15. $vvv = implode(',',$dd);
  16. $bind = "\$stmt->bind_param('$ty',$vvv);";
  17. $key = implode(',',$kk);
  18. $value = implode(',',$vv);
  19. $stmt = $m->prepare("insert into $tn($key) values($value)");
  20. eval($bind);
  21. $stmt->execute();
  22. $stmt->free_result();
  23. }
插入数据预处理语句函数的使用
  1. //方法一修改数据也是同样的方法只是多了一个条件
  2. $d['sname']='你bu好';
  3. $d['sscore']=90;
  4. $d['sgender']='保密';
  5. save('stu',$d);
  6. //方法二
  7. save('stu',array('sname'=>'好人'));






Copyright © 2024 小蕊同学
Powered by .NET 9.0 on Kubernetes