预处理语句获得总记录数的函数及其调用

Posted on 2015-06-05 00:46  小蕊同学  阅读(198)  评论(0编辑  收藏  举报
预处理查询函数
  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为表名,$w为条件
  10. function get_count($tn,$w='1=1'){
  11. global $m;
  12. $stmt=$m->prepare("select count(*) from $tn where $w");
  13. $stmt->execute();
  14. $stmt->bind_result($c);
  15. $stmt->fetch();
  16. $stmt->free_result();
  17. $stmt->close();
  18. return $c;
  19. }
查询函数的使用
  1. $re=get_count('stu','sscore=90');
  2. echo $re;





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