预处理语句查询函数

Posted on 2015-06-05 00:38  小蕊同学  阅读(297)  评论(0编辑  收藏  举报
$tn为表明,$f为查询的字段名,不写则为全部,$w为条件,$o为按照顺序排序,$l为显示几条信息
预处理查询函数
  1. <?php
  2. $host = 'localhost';
  3. $user = 'root';
  4. $pass = '';
  5. $dbname = 'db';
  6. $charset = 'utf8';
  7. $m = new mysqli($host,$user,$pass,$dbname);
  8. $m->set_charset($charset);
  9. //$tn为表明,$f为查询的字段名,不写则为全部,$w为条件,$o为按照顺序排序,$l为显示几条信息
  10. function select($tn,$f='*',$w='1=1',$o='',$l=0){
  11. global $m;
  12. if($l<=0){
  13. $stmt=$m->prepare("select $f from $tn where $w $o");
  14. }else{
  15. $stmt=$m->prepare("select $f from $tn where $w $o limit $l");
  16. }
  17. $stmt->execute();
  18. $rows=$stmt->get_result()->fetch_all(2);
  19. $stmt->free_result();
  20. $stmt->close();
  21. return $rows;
  22. }
预处理查询函数的使用
  1. //查询语句函数的使用$tn为表明,$f为查询的字段名,不写则为全部,$w为条件,$o为按照顺序排序,$l为显示几条信息
  2. $re=select('stu','sname,sscore','sscore>10','order by sscore asc',5 );
  3. echo '<pre>';
  4. var_dump($re);






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