zoukankan      html  css  js  c++  java
  • 使用预处理语句实现数据查询的方法

    查询数据库里面有多少条数据
    $m=new mysqli('localhost','root','','db');
    $m->set_charset('utf8');
    $stmt=$m->prepare('select count(*) from stu');
    $stmt->execute();
    $stmt->bind_result($c);
    $stmt->fetch();
    echo $c;
    $stmt->free_result();
    $stmt->close();
    $m->close();
    使用预处理语句实现数据的查询方法一:
    1. $m=new mysqli('localhost','root','','db');
    2. $m->set_charset('utf8');
    3. $stmt=$m->prepare('select * from stu where 1=1');
    4. $stmt->execute();
    5. $stmt->bind_result($id,$name,$sgender,$sscore);
    6. while($stmt->fetch()){
    7. echo "$id,$name,$sgender,$sscore".'<br>';
    8. }
    9. $stmt->free_result();
    10. $stmt->close();
    11. $m->close();
    使用预处理语句实现数据的查询方法二:
    1. $m=new mysqli('localhost','root','','db');
    2. $m->set_charset('utf8');
    3. $stmt=$m->prepare('select * from stu where 1=1');
    4. $stmt->execute();
    5. $result=$stmt->get_result();
    6. $rows=$result->fetch_all(2);
    7. foreach($rows as $v){
    8. print_r($v).'<br>';
    9. }
    10. $stmt->free_result();
    11. $stmt->close();
    12. $m->close();
    使用预处理语句实现数据的查询方法三:
    $m=new mysqli('localhost','root','','db');
    $m->set_charset('utf8');
    $stmt=$m->prepare('select * from stu where sid=?');
    $n=10;
    $stmt->bind_param('i',$n);
    $stmt->execute();
    $stmt->bind_result($id,$name,$sgender,$sscore);
    $stmt->fetch();
    echo $id,$name,$sgender,$sscore;
    $stmt->free_result();
    $stmt->close();
    $m->close();




  • 相关阅读:
    选择排序法
    计算大阶乘(值很大)
    递归计算阶乘
    计算m的m次方倒数的和
    使用文件(FILE)输入输出
    判断回文数
    最小公倍数(调用函数与全局变量)
    How to 共用体
    How to 枚举
    有效编写软件的75条建议(转)
  • 原文地址:https://www.cnblogs.com/lsr111/p/4544801.html
Copyright © 2011-2022 走看看