zoukankan      html  css  js  c++  java
  • Connect.class.php连接数据库(随时修改)

    <?php
    class Connect
    {
        public $host = "localhost"; //服务器地址
        public $uid = "root"; //数据库的用户名
        public $pwd = "123"; //数据库的密 
        
        //执行SQL语句,返回相应结果的函数
        //$sql是要执行的SQL语句
        //$type是SQL语句的类型,0代表增删改,1代表查询
        //$db代表要操作的数据库
        
        
            //返回json数据
             public function  PDO_json($sql,$type=1,$db="mydb")
        {
                  try
             {
                $dsn = "mysql:dbname=mydb;host=localhost";
                $pdo = new PDO($dsn,$this->uid,$this->pwd);
             }
                  
                  catch(Exception $e)
             {  
                  echo "连接失败".$e->getMessage();      
             }
                
                $st = $pdo->prepare($sql);
                
                $result = $st->execute();
                
                if($type==1)
              {
                $att = $st->fetchAll(PDO::FETCH_ASSOC);
                
                return json_encode($att);
              }
                  else
              {
                    if($result)
                {
                    return "OK";
                }
                        else
                {
                    return "NO";
                }
              }
        }
            
            //返回二维数组
             public function  PDO_array($sql,$type=1,$db="mydb")
        {
                  try
             {
                $dsn = "mysql:dbname=mydb;host=localhost";
                $pdo = new PDO($dsn,$this->uid,$this->pwd);
             }
                  catch(Exception $e)
             {  
                  echo "连接失败".$e->getMessage();      
             }
                
                $st = $pdo->prepare($sql);
                
                $result = $st->execute();
                
                  if($type==1)
              {
                return $st->fetchAll(PDO::FETCH_NUM);
              }
                  else
              { 
                  //如果是其他语句,返回true或false
                    if($result)
                {
                    return "OK";
                }
                    else
                {
                    return "NO";
                }
              }
        }
        
                
    
         //返回字符串    
             public function PDO_string($sql,$type=1,$db="mydb")
        {    
        
                    try
               {
                  $dsn = "mysql:dbname=mydb;host=localhost";
                  $pdo = new PDO($dsn,$this->uid,$this->pwd);
               }
               
                       catch(Exception $e)
         
               {  
                    echo "连接失败".$e->getMessage();      
               }
                
                    $st = $pdo->prepare($sql);
                    
                    $result = $st->execute();
                
                  if($type==1)
              {
                  return $st->fetchColumn();
              }
                  else
              {
                        if($result)
                {
                    return "OK";
                }
                    else
                {
                    return "NO";
                }
              }
        }
    }
    ?>
    View Code
  • 相关阅读:
    C++操作Kafka使用Protobuf进行跨语言数据交互
    聊聊Disruptor 和 Aeron 这两个开源库
    DTrace arg0-kernel mode and arg1-user mode
    top
    how to write your first linux device driver
    how to compile and replace ubuntu kernel
    linux du
    c++ rvo vs std::move
    【Android】wifi开发
    无线局域网络 WIFI/WAPI/WLAN区别浅析
  • 原文地址:https://www.cnblogs.com/sihuiming/p/5342911.html
Copyright © 2011-2022 走看看