zoukankan      html  css  js  c++  java
  • 数据库连接和设置过程

    <?phpclass mysql{
        private $host;  //服务器地址
        private $name;  //用户名称
        private $pass;  //密码
        private $table; //连接数据库教程
        private $jiema; //设置解码
        private $ztime; //设置服务器的时区
        //构造函数
        function __construct($host,$name,$pass,$table,$jiema,$ztime){
            $this -> host  = $host  ;
          $this -> name  = $name  ;
          $this -> pass  = $pass  ;
          $this -> table = $table ;
      $this -> jiema = $jiema ;
      $this -> ztime = $ztime ;
          $this -> connect();
         }
         //数据库连接和设置
         function connect(){
             $link=@mysql_connect($this->host,$this->name,$this->pass) or die ("连接服务器失败");
             @mysql_select_db($this->table,$link) or die("连接数据失败");
             @mysql_query("set names '$this->jiema'");
          @date_default_timezone_set("$this->ztime");
         }
        //执行操作
     function query($sql) {
         if(!($query = @mysql_query($sql))) $this->show($sql);
      return $query;
     }
        //显示信息
        function show($message = '', $sql = '') {
      if(!$sql) echo $message;
      else echo $message.'<br>'.$sql;
     }
        //取得数据集的某个值
     function result($query,$row,$values) {2881064151
      return @mysql_result($query,$row,$values);
     }
        //取得数据集的某个值
     function get_values($table,$row,$values) {
         $query = $this -> query("select * from $table");
      $returnvalues = mysql_result($query,$row,$values);
      return $returnvalues;
     }
        //取得数据集的行数
     function num_rows($query) {
      return @mysql_num_rows($query);
     }
        //循环读取数据
     function fetch($query) {
      return @mysql_fetch_array($query);
     }
        //最后一次插入纪录的id值
     function insert_id() {
      return mysql_insert_id();
     }
        //取得数据集中的一行
     function fetch_row($query) {
      return mysql_fetch_row($query);
     }
        //插入一条数据
        function fn_insert($table,$name,$value){
         if($this->query("insert into $table ($name) values ($value)")){
          return true;
      }else{
          return false;
      }
        }
        //插入任意数据
        function sql_insert($tbname,$postvalues){
            foreach ($postvalues as $key => $value) {
       $postvalue .= "`".$key."`".",";
       $sqlvalue .= "'".$value."',";
      }
      $sqlfield = mb_substr("$postvalue",0,-1,'gbk');
      $sqlvalue = mb_substr("$sqlvalue",0,-1,'gbk');
      if($this-> fn_insert("$tbname","$sqlfield","$sqlvalue")){
          return true;
      }else{
          return false;
      }
     } 
        //修改万能数据
        function sql_update($table,$postvalues,$wwhere){
      foreach ($postvalues as $key=>$value) {
       $sqlfield .= $key."="."'".$value."'".",";
      }
      $sqlfield= mb_substr("$sqlfield",0,-1,'gbk');
      if($this->fn_update("$table","$sqlfield","$wwhere")){
          return true;
      }else{
          return false;
      }
     }
        //修改一条数据
        function fn_update($table,$value,$wwhere){
         if($this->query("update $table set $value where $wwhere")){
          return true;
         }else{
          return false;
      }
        }
        //删除一条数据
        function sql_delete($table,$wwhere){
         if($this->query("delete from $table where $wwhere")){
          return true;
      }else{
          return false;
      }
     }
     //关闭数据连接
        function close() {
      return mysql_close();
     }
    }
    $db =  new mysql($location['host'],$location['hostname'],$location['hostpass'],$location['table'],$location['jiema'],$location['ztime']);    
    ?>

  • 相关阅读:
    1.python简介
    JSP标准标签库:JSTL
    冒泡排序算法
    中英文金额大写转换器
    递归与斐波那契数列
    web.xml配置文件详解
    Servlet及相关类和接口
    Servlet初始化及处理HTTP请求
    [转]jqGrid 属性、事件全集
    java web 过滤器跟拦截器的区别和使用
  • 原文地址:https://www.cnblogs.com/cbryge/p/5995342.html
Copyright © 2011-2022 走看看