<?php
/*********************************************************************************
--                     福州陆海建设科技有限公司
--                    
--                    Copyright (c) 2017 曲洪岩
--                    
--                    QQ:28007540 
-- 
--                     
*********************************************************************************/

class db
{
    var $Host = "";
    var $Database = "";
    var $User = "";
    var $Password = "";


    var $Auto_Free = 1; ## Set to 1 for automatic mysql_free_result()
    var $Debug = 0; ## Set to 1 for debugging messages.
    var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
    var $Seq_Table = "db_sequence";

    /* public: result array and current row number */
    var $Record = array();
    var $Row;

    /* public: current error number and error text */
    var $Errno = 0;
    var $Error = "";

    /* public: this is an api revision, not a CVS revision. */
    var $type = "mysql";
    var $revision = "1.2";

    /* private: link and query handles */
    var $Link_ID = 0;
    var $Query_ID = 0;

    var $insert_id;
    var $table_head  = 'tb_';
    var $view_head  = 'v_';

    /**************************************
    --             函数说明
    --     功能：初始化数据库连接信息；
    --     参数：
    --     返回值：
    ***************************************/
    public function __construct()
    {
        global $GO_CONFIG;
        $this->Host = $GO_CONFIG->db_host;
        $this->Database = $GO_CONFIG->db_name;
        $this->User = $GO_CONFIG->db_user;
        $this->Password = $GO_CONFIG->db_passwd;
    }
function link_id()
    {
        return $this->Link_ID;
    }

    function query_id()
    {
        return $this->Query_ID;
    }

    /**************************************
    --           函数说明
    --     功能：连接数据库；
    --     参数：数据库 主机 用户名 密码
    --     返回值：
    ***************************************/
    function connect($Database = "", $Host = "", $User = "", $Password = "")
    {
        /* Handle defaults */
        if ("" == $Database)
            $Database = $this->Database;
        if ("" == $Host)
            $Host = $this->Host;
        if ("" == $User)
            $User = $this->User;
        if ("" == $Password)
            $Password = $this->Password;

        if (0 == $this->Link_ID) {
            $this->Link_ID = mysql_connect($Host, $User, $Password);
            if (!$this->Link_ID) {
                return 0;
            }

            if (!@mysql_select_db($Database, $this->Link_ID)) {
                return 0;
            }
        }
        mysql_query("SET NAMES 'UTF8'"); 
        mysql_query("SET CHARACTER SET UTF8"); 
        mysql_query("SET CHARACTER_SET_RESULTS=UTF8'");
        return $this->Link_ID;
    }

    /**************************************
    --           函数说明
    --     功能：执行存储过程；
    --     参数；
    ***************************************/
    function proceduce_exc($proceduceName)
    {
        $Query_String = 'call ' . $proceduceName.';';
       
        if ($Query_String == "") return 0;

        if (!$this->connect()) return 0;
        
        if ($this->Query_ID)  $this->free();
         if ($this->Debug)
            printf("Debug: query = %s<br>\n", $Query_String);

        $this->Query_ID = mysql_query($Query_String, $this->Link_ID);
         $this->Row = 0;
                 $this->Errno = mysql_errno();
        $this->Error = mysql_error();
             if (!$this->Query_ID) {
            $this->halt("Invalid SQL:" . $Query_String);
        }
        // Will return nada if it fails. That's fine.
        $this->insert_id =  mysql_insert_id();
        return $this->Query_ID;
    }
        /**************************************
    --           函数说明
    --     功能：执行存储过程；
    --     参数；
    ***************************************/
    function sql_exc($Query_String)
    {
      
       
        if ($Query_String == "") return 0;

        if (!$this->connect()) return 0;
        
        if ($this->Query_ID)  $this->free();
         if ($this->Debug)
            printf("Debug: query = %s<br>\n", $Query_String);

        $this->Query_ID = mysql_query($Query_String, $this->Link_ID);
         $this->Row = 0;
                 $this->Errno = mysql_errno();
        $this->Error = mysql_error();

             if (!$this->Query_ID) {
            $this->halt("Invalid SQL:" . $Query_String);
        }
        // Will return nada if it fails. That's fine.
        $this->insert_id =  mysql_insert_id();
        return $this->Query_ID;
    }
    /**************************************
    --           函数说明
    --     功能：获取单条记录；
    --     参数；
    ***************************************/
    function next_record()
    {
        if (!$this->Query_ID) {
            return 0;
        }

        $this->Record = @mysql_fetch_array($this->Query_ID);
        $this->Row += 1;
          
        
        $this->Errno = mysql_errno();
        $this->Error = mysql_error();
        $stat = is_array($this->Record);
        if (!$stat && $this->Auto_Free) {
            $this->free();
        }
        return $stat;
        
    }
    function limitStr($pageNo,$pageNum)
    {
        $sql = ' limit '.(($pageNo-1)*$pageNum).','.$pageNum;
        return $sql;
    }
    /**************************************
    --           函数说明
    --     功能：查找数据项；
    --     参数；
    ***************************************/
    function f($Name)
    {
        return $this->Record[$Name];
    }
    /**************************************
    --           函数说明
    --     功能：释放资源；
    --     参数；
    ***************************************/
    function free()
    {
      
        @mysql_free_result($this->Query_ID);
        @mysql_close($this->Link_ID);
        $this->Query_ID = 0;
    }

    function write_log($msg)
    {
        
    	$fp = fopen("/tmp/web1.log","aw+");
    	fwrite($fp,$msg);
       
    	fclose($fp);
        
        
    }
}

?>
