zoukankan      html  css  js  c++  java
  • 链接数据库 类

     1 <?php
     2 
     3 
     4 class SQL
     5 {
     6     public $host="localhost"; //数据库地址    
     7     public $uid="root"; //数据库用户名
     8     public $pwd="";  //数据库密码
     9     
    10     //Mysql方法
    11     //执行SQL语句的方法
    12     //参数:$sql要执行的SQL语句,$type是SQL语句类型0代表查询1代表其他,$db代表要操作的数据库
    13     function Query($sql,$type=0,$db="mydb")
    14     {
    15         //1.造链接对象
    16         $dbconnect =new MySQLi($this->host,$this->uid,$this->pwd,$db);
    17         //2.判断链接是否出错
    18         !mysqli_connect_error()  or die ("链接失败");
    19         //3.执行SQL语句
    20         $result=$dbconnect->query($sql);
    21         if($type==0)
    22         {
    23             return $result->fetch_all();
    24         }
    25         else
    26         {
    27             return $result;    
    28         }
    29     }
    30     
    31     
    32     
    33     //PDO方法
    34     //参数:$sql要执行的SQL语句,$type是SQL语句类型0代表查询1代表其他,$qd代表驱动名,$db代表要操作的数据库
    35     function Prepare($sql,$qd="mysql",$db="mydb")
    36     {
    37         //1.造链接对象    
    38         $pdo=new PDO("$qd:dbname=$db;host=$this->host",$this->uid,$this->pwd);
    39         //2.准备一条SQL语句
    40         $stm=$pdo->prepare($sql);
    41         //3.执行准备好的SQL语句,成功返回true ,失败返回false
    42         if($stm->execute())
    43         {
    44             return $stm->fetchAll();
    45         }
    46         else
    47         {
    48             echo "执行失败";
    49         }
    50      }
    51         
    52 }
  • 相关阅读:
    C#中async/await中的异常处理
    Xunit
    Markdown安装与简单使用
    .Net Ajax跨域请求总结
    centos 安装 Vmare tool
    linux安装Java
    linux常用命令整理
    autofac解析Mvc和Webapi的坑
    swarm on ubuntu
    deploy service on swarm
  • 原文地址:https://www.cnblogs.com/zbxiaoxu/p/5485557.html
Copyright © 2011-2022 走看看