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
  • 相关阅读:
    vue_组件化开发
    C++ / C# 访问网络共享文件夹
    PetaLinux 设置操作系统内存
    linux 不用./ 直接执行程序
    Visual Studio Code 开发环境搭建 —— C# 扩展插件
    Visual Studio Code 调试项目时传参
    PetaLinux 安装
    Ubuntu 报 "xxx is not in the sudoers file.This incident will be reported" 错误解决方法
    常用 Linux 命令
    搭建 Git 服务器(Ubuntu 系统)
  • 原文地址:https://www.cnblogs.com/sihuiming/p/5342911.html
Copyright © 2011-2022 走看看