PHP的SqlHelp

來源:互聯網
上載者:User
pre = $pre;                $this->connect($host, $user, $pwd, $db);                Mysql::$self =  $this;        }                /**         * @param string $host         * @param string $host         * @param string $pwd         * @param string $db         * @param string $pre         * @return DB         */        public static function init($host='', $user='', $pwd='', $db='', $pre = '') {                if(!(Mysql::$self instanceof DB)){                          Mysql::$self = new Mysql($host, $user, $pwd, $db, $pre);                }                return Mysql::$self;        }        /**         * 建立串連         */        private function connect($host, $user, $pwd, $db, $pre = '') {                $this->pre = $pre;                if(! $this->link = mysql_connect($host, $user, $pwd)) die(                        'Connection to mysql server could not be established.');                if(! mysql_select_db($db, $this->link)) die(                        'Database could not be selected.');        }        /**         * 執行sql         * @param  string $sql         * @return Mysql          */        public function query($sql) {                $this->times ++;                $this->begintime = microtime();                mysql_query('SET NAMES '.self::$charset, $this->link);                $this->sql = $sql;                $this->q = mysql_query($sql, $this->link);                $this->alltime[] = array('t' => microtime() - $this->begintime,                        's' => $sql);                return $this;        }        /**         * 擷取單個查詢值         * @param  string $sql         * @return mixed         */        public function getOne($sql = '') {                if(strlen(trim($sql)) > 0){                        $this->query($sql);                }                if($this->check()){                        $result = mysql_fetch_array($this->q, MYSQL_NUM);                        mysql_free_result($this->q);                        return $result[0];                }                return false;        }        /**         * 擷取一條記錄         * @param  string $sql         * @return array         */        public function getRow($sql = '') {                if(strlen(trim($sql)) > 0){                        $this->query($sql);                }                if($this->check()){                        $result = mysql_fetch_assoc($this->q);                        mysql_free_result($this->q);                        return $result;                }else{                        return array();                }        }        /**         * 擷取所有查詢         * @param  string $sql         * @return array         */        public function getAll($sql = '') {                if(strlen(trim($sql)) > 0){                        $this->query($sql);                }                $return = array();                if($this->check()){                        while($temp = mysql_fetch_assoc($this->q))                                $return[] = $temp;                        $this->rows = mysql_num_rows($this->q);                        mysql_free_result($this->q);                        return $return;                }else{                        return array();                }        }        /**         * 擷取插入ID         * @return int         */        public function InsertId($seq=0) {                return mysql_insert_id($this->link);        }        /**         * 擷取影響行數         * @return  int         */        public function rowNummber() {                return $this->rows;        }        /**         * 檢查查詢是否成功         * @return bool         */        public function check() {                return $this->q === false ? false : true;        }                /**         * 獲得串連         * @return resource         */        public function getLink() {                return $this->link;        }        /**         * 獲得本次串連的查詢語句         * @return string         */        public function getSQL() {                return $this->sql;        }        /**         * 獲得類名         * @return string         */        public function __toString() {                return __CLASS__;        }        /**         * 獲得執行時間         * @return float         */        public function getTimeInfo() {                return $this->alltime;        }        /**         * 獲得執行次數         * @return Int         */        public function getTimes() {                return $this->times;        }        /**         * 獲得結果行數         * @return Int         */        public function getResultRows() {                $rows = mysql_num_rows($this->q);                mysql_free_result($this->q);                return $rows;        }        /**         * $Id: Mysql.php 685 2009-12-03 10:49:04Z HH.Sun $         * 獲得最後執行語句的錯誤         * @desc         getLastError         * @access       public         * @return        string         * @version      $Revision: 685 $         */        public function getLastError(){                return mysql_error($this->link);        }        /**         * $Id: Mysql.php 685 2009-12-03 10:49:04Z HH.Sun $         * mysql_real_escape_string 調用         * @desc          escape         * @access      public         * @param       string $sql         * @return       string         *         * @since         2009-9-25 下午02:06:41         * @version     $Revision: 685 $         */        public function escape($sql){                return mysql_real_escape_string($sql,$this->link);        }}?>  初始化:getOne($sql);$line  = $db->getRow($sql);$all    = $db->getAll($sql);//其他檔案global $db;$sql = "................................................";$one = $db->getOne($sql);$line  = $db->getRow($sql);$all    = $db->getAll($sql);?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.