Maison développement back-end tutoriel php php实战第五天_PHP教程

php实战第五天_PHP教程

Jul 14, 2016 am 10:10 AM
php 使用方法 创建 变量 ciel 实战 理解 设计模式 conduire statique

php单态设计模式,我的理解,类中创建静态变量,使其只有一个,使用方法进行返回对象,该方法 检查对象是存在不存在就创建。从而实现单一对象。

将__construct()设置为private这样就new不了咯.self是本类的意思。“::”调用静态方法

 

 


[php]      /**
    * 
    */ 
    class myclass 
    { 
        static private $db_class; 
        static public function getClass() 
        { 
            if (is_null(self::$db_class)) { 
                self::$db_class=new myclass(); 
                echo "创建对象咯"; 
            }else{ 
                echo "返回原来的对象"; 
                return self::$db_class; 
            } 
        } 
        private function __construct() 
        { 
            echo "11111111111111"; 
        } 
 
        public function __destruct(){ 
            echo "
aaaaaaaaaaaa"; 
        } 
        public function show(){ 
            echo "bbbbbbbbbbbbbbb"; 
        } 
    } 
    $a=myclass::getClass(); 
    $a=myclass::getClass(); 
    $a=myclass::getClass(); 
    $a=myclass::getClass(); 
    $a=myclass::getClass(); 
    $a=myclass::getClass(); 
    $a=myclass::getClass(); 
    $a=myclass::getClass(); 
    $a=myclass::getClass(); 
    $a=myclass::getClass(); 
    $a->show(); 
 
 
    /*//直接new 会报错的噢!!!
    new myclass();
    new myclass();
 
    new myclass();
    new myclass();
 
    new myclass();
    new myclass();
    new myclass();
    new myclass();*/ 
 ?> 

 /**
 *
 */
 class myclass
 {
  static private $db_class;
  static public function getClass()
  {
   if (is_null(self::$db_class)) {
    self::$db_class=new myclass();
    echo "创建对象咯";
   }else{
    echo "返回原来的对象";
    return self::$db_class;
   }
  }
  private function __construct()
  {
   echo "11111111111111";
  }

  public function __destruct(){
   echo "
aaaaaaaaaaaa";
  }
  public function show(){
   echo "bbbbbbbbbbbbbbb";
  }
 }
 $a=myclass::getClass();
 $a=myclass::getClass();
 $a=myclass::getClass();
 $a=myclass::getClass();
 $a=myclass::getClass();
 $a=myclass::getClass();
 $a=myclass::getClass();
 $a=myclass::getClass();
 $a=myclass::getClass();
 $a=myclass::getClass();
 $a->show();


 /*//直接new 会报错的噢!!!
 new myclass();
 new myclass();

 new myclass();
 new myclass();

 new myclass();
 new myclass();
 new myclass();
 new myclass();*/
 ?>

 

 

 

 

 

 

这个是小例子.下面给出 单态的 mysql类


[php]  // +----------------------------------------------------------------------  
// |MySQL操作类  
// +----------------------------------------------------------------------  
// |@微凉 QQ:496928838  
// +----------------------------------------------------------------------  
class MySQL{ 
     
    private $db_mysql_hostname; 
    private $db_mysql_username; 
    private $db_mysql_password; 
    private $db_mysql_database; 
    private $db_mysql_port; 
    private $db_mysql_charset; 
     
    private $query_list = array(); 
     
    //查询次数  
    public $query_count = 0; 
    //查询开始时间  
    public $query_start_time; 
     
    //当前查询ID  
    protected $queryID; 
    //当前连接  
    protected $conn; 
    // 事务指令数  
    protected $transTimes = 0; 
    // 返回或者影响记录数  
    protected $numRows    = 0; 
    // 错误信息  
    protected $error      = ''; 
     
    //静态实例  
    static private $db_class; 
    //取得本类  
    static public function getClass() 
    { 
      if (is_null(self::$db_class)) { 
            self::$db_class=new MySQL(); 
        } 
        return self::$db_class; 
    } 
 
    //定义为私有方法就new起来咯,要用 getClass  
    private function __construct() 
    { 
             
    } 
 
 
    public function connect($hostname_or_conf,$username,$password,$database,$port = '3306',$char = 'utf8'){ 
        if(is_array($hostname_or_conf)){ 
            $this->db_mysql_hostname = $hostname_or_conf['hostname']; 
            $this->db_mysql_username = $hostname_or_conf['username']; 
            $this->db_mysql_password = $hostname_or_conf['password']; 
            $this->db_mysql_database = $hostname_or_conf['database']; 
            $this->db_mysql_port = isset($hostname_or_conf['port'])?$hostname_or_conf['port']:'3306'; 
            $this->db_mysql_charset = isset($hostname_or_conf['charset'])?$hostname_or_conf['charset']:'utf8'; 
        }elseif(!empty($hostname_or_conf)||!empty($username)||!empty($password)||!empty($database)) 
        { 
             $this->db_mysql_hostname = $hostname_or_conf; 
             $this->db_mysql_username = $username; 
             $this->db_mysql_password = $password; 
             $this->db_mysql_database = $database; 
             $this->db_mysql_port = $port; 
             $this->db_mysql_charset = $char; 
              
        }else{ 
            die('configuration error.'); 
        } 
         
        $server = $this->db_mysql_hostname.':'.$this->db_mysql_port; 
        $this->conn = mysql_connect($server,$this->db_mysql_username,$this->db_mysql_password,true) or die('Connect MySQL DB error!'); 
        mysql_select_db($this->db_mysql_database,$this->conn) or die('select db error!'); 
        mysql_query("set names " . $this->db_mysql_charset, $this->conn); 
 
    } 
 
 
 
 
    /**
     +----------------------------------------------------------
     * 设置数据对象值
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     *table,where,order,limit,data,field,join,group,having
     +----------------------------------------------------------
     */ 
    public function table($table){ 
        $this->query_list['table'] = $table; 
        return $this; 
    } 
     
    public function where($where){ 
        $this->query_list['where'] = $where; 
        return $this; 
    } 
     
    public function order($order){ 
        $this->query_list['order'] = $order; 
        return $this; 
    } 
     
    public function limit($offset,$length=null){ 
        if (is_null($length)) { 
            $this->query_list['limit']='limit '.$offset; 
            return $this; 
        }else { 
            if(!isset($length)){ 
                $length = $offset; 
                $offset = 0; 
            } 
                $this->query_list['limit'] = 'limit '.$offset.','.$length; 
                return $this; 
        } 
 
    } 
     
    public function data($data){ 
        //读取数据表字段,然后处理表单数据  
        $dataList = $this->getFields($this->query_list['table']); 
        $arr=array(); 
        foreach ($dataList as $key=>$value) { 
            if (array_key_exists ($key,$data) ) { 
                $arr[$key]=$data[$key]; 
            } 
             
        } 
        //var_dump($arr);  
        /*
        if(is_object($data)){
            $data   =   get_object_vars($data);
        }elseif (is_string($data)){
            parse_str($data,$data);
        }elseif(!is_array($data)){
            //Log:DATA_TYPE_INVALID
        }
        */ 
        $this->query_list['data'] = $arr; 
        return $this; 
    } 
    public function field($fields){ 
        $this->query_list['fields'] = $fields; 
        return $this; 
    } 
    public function join($join){ 
        $this->query_list['join'] = $join; 
        return $this; 
    } 
    public function group($group){ 
        $this->query_list['group'] = $group; 
        return $this; 
    } 
    public function having($having){ 
        $this->query_list['having'] = $having; 
        return $this; 
    } 
    /**
     +----------------------------------------------------------
     * 查询
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param 
     +----------------------------------------------------------
     */ 
    public function select(){ 
        $select_sql = 'select '; 
        $fields = isset($this->query_list['fields'])?$this->query_list['fields']:'*'; 
        $select_sql.=$fields; 
        $select_sql.= ' from `'.$this->query_list['table'].'` '; 
         
        isset($this->query_list['join'])?($select_sql.=$this->query_list['join']):''; 
        isset($this->query_list['where'])?($select_sql.=' where '.$this->query_list['where']):''; 
        isset($this->query_list['group'])?($select_sql.=' group by'.$this->query_list['group']):''; 
        isset($this->query_list['having'])?($select_sql.=' mysql having '.$this->query_list['having']):''; 
        isset($this->query_list['order'])?($select_sql.=' order by '.$this->query_list['order']):''; 
        isset($this->query_list['limit'])?($select_sql.=' '.$this->query_list['limit']):''; 
    //  echo '----->'.$select_sql;  
        return $this->query($select_sql); 
    } 
    /**
     +----------------------------------------------------------
     * 增加
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param 
     +----------------------------------------------------------
     */ 
    public function add(){ 
        $add_sql = 'insert into `'.$this->query_list['table'].'` ('; 
         
        $data = $this->query_list['data']; 
        $value = $field = ''; 
        foreach($data as $k=>$v){ 
            $field .= '`'.$k.'`,'; 
            if(is_numeric($v)) 
                $value .= $v.','; 
            else 
                $value .= '\''.$v.'\','; 
        } 
        $add_sql .= rtrim($field,',').') values ('.rtrim($value,',').')'; 
 
    //  echo 'add_sql'.$add_sql;  
        return $this->execute($add_sql); 
    } 
    /**
     +----------------------------------------------------------
     * 删除
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param 
     +----------------------------------------------------------
     */ 
    public function delete(){ 
        $del_sql = 'delete from `'.$this->query_list['table'].'` where '.$this->query_list['where']; 
         
        if(isset($this->query_list['order'])) 
            $del_sql .= 'order by '.$this->query_list['order']; 
        if(isset($this->query_list['limit'])) 
            $del_sql .= ' '.$this->query_list['limit']; 
             
        return $this->execute($del_sql); 
         
    } 
    /**
     +----------------------------------------------------------
     * 更新
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param 
     +----------------------------------------------------------
     */ 
    public function update(){ 
        $update_sql = 'update `'.$this->query_list['table'].'` set '; 
        $data = $this->query_list['data']; 
         
        foreach($data as $k=>$v){ 
            if(is_numeric($v)) 
                $update_sql .= '`'.$k.'` ='.$v.','; 
            else 
                $update_sql .= '`'.$k.'` =\''.$v.'\','; 
        } 
        $update_sql = rtrim($update_sql,','); 
        if(isset($this->query_list['where'])) 
            $update_sql .= ' where '.$this->query_list['where']; 
        if(isset($this->query_list['order'])) 
            $update_sql .= ' order by '.$this->query_list['order']; 
        if(isset($this->query_list['limit'])) 
            $update_sql .= ' '.$this->query_list['limit']; 
         
        return $this->execute($update_sql); 
         
    } 
     /**
     +----------------------------------------------------------
     * 执行查询 返回数据集
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $sql  sql指令
     */ 
    public function query($sql) { 
        if ( !$this->conn ) return false; 
        $this->queryStr = $sql; 
        //释放前次的查询结果  
        if ( $this->queryID ) {    $this->free();    } 
         
        $this->query_start_time = microtime(true); 
         
        $this->queryID = mysql_query($sql, $this->conn); 
        $this->query_count++; 
        if ( false === $this->queryID ) { 
            $this->error(); 
            return false; 
        } else { 
            $this->numRows = mysql_num_rows($this->queryID); 
            return $this->getAll(); 
        } 
    } 
    /**
     +----------------------------------------------------------
     * 执行语句
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $sql  sql指令
     +----------------------------------------------------------
     */ 
    public function execute($sql) { 
        if ( !$this->conn ) return false; 
        $this->queryStr = $sql; 
        //释放前次的查询结果  
        if ( $this->queryID ) {    $this->free();    } 
         
        $this->query_start_time = microtime(true); 
         
        $result =   mysql_query($sql, $this->conn) ; 
        $this->query_count++; 
        if ( false === $result) { 
            $this->error(); 
            return false; 
        } else { 
            $this->numRows = mysql_affected_rows($this->conn); 
            return $this->numRows; 
        } 
    } 
    /**
     +----------------------------------------------------------
     * 获得所有的查询数据
     +----------------------------------------------------------
     * @access private
     +----------------------------------------------------------
     * @return array
     */ 
    private function getAll() { 
        //返回数据集  
        $result = array(); 
        if($this->numRows >0) { 
            while($row = mysql_fetch_assoc($this->queryID)){ 
                $result[]   =   $row; 
            } 
            mysql_data_seek($this->queryID,0); 
        } 
        return $result; 
    } 
    /**
     +----------------------------------------------------------
     * 取得数据表的字段信息
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */ 
    public function getFields($tableName) { 
        $result =   $this->query('SHOW COLUMNS FROM `'.$tableName.'`'); 
        $info   =   array(); 
        if($result) { 
            foreach ($result as $key => $val) { 
                $info[$val['Field']] = array( 
                    'name'    => $val['Field'], 
                    'type'    => $val['Type'], 
                    'notnull' => (bool) ($val['Null'] === ''), // not null is empty, null is yes  
                    'default' => $val['Default'], 
                    'primary' => (strtolower($val['Key']) == 'pri'), 
                    'autoinc' => (strtolower($val['Extra']) == 'auto_increment'), 
                ); 
            } 
        } 
        return $info; 
    } 
    /**
     +----------------------------------------------------------
     * 取得数据库的表信息
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */ 
    public function getTables($dbName='') { 
        if(!empty($dbName)) { 
           $sql    = 'SHOW TABLES FROM '.$dbName; 
        }else{ 
           $sql    = 'SHOW TABLES '; 
        } 
        $result =   $this->query($sql); 
        $info   =   array(); 
        foreach ($result as $key => $val) { 
            $info[$key] = current($val); 
        } 
        return $info; 
    } 
 
    /**
     +----------------------------------------------------------
     * 最后次操作的ID
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param 
     +----------------------------------------------------------
     */ 
     public function last_insert_id(){ 
        return mysql_insert_id($this->conn); 
    } 
    /**
     * 执行一条带有结果集计数的
     */ 
    public function count($sql){ 
        return $this->execute($sql); 
    } 
    /**
     +----------------------------------------------------------
     * 启动事务
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */ 
    public function startTrans() { 
        if ($this->transTimes == 0) { 
            mysql_query('START TRANSACTION', $this->conn); 
        } 
        $this->transTimes++; 
        return ; 
    } 
 
    /**
     +----------------------------------------------------------
     * 提交事务
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return boolen
     +----------------------------------------------------------
     */ 
    public function commit() 
    { 
        if ($this->transTimes > 0) { 
            $result = mysql_query('COMMIT', $this->conn); 
            $this->transTimes = 0; 
            if(!$result){ 
                throw new Exception($this->error()); 
            } 
        } 
        return true; 
    } 
 
    /**
     +----------------------------------------------------------
     * 事务回滚
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return boolen
     +----------------------------------------------------------
     */ 
    public function rollback() 
    { 
        if ($this->transTimes > 0) { 
            $result = mysql_query('ROLLBACK', $this->conn); 
            $this->transTimes = 0; 
            if(!$result){ 
                throw new Exception($this->error()); 
            } 
        } 
        return true; 
    } 
    /**
     +----------------------------------------------------------
     * 错误信息
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param 
     +----------------------------------------------------------
     */ 
     public function error() { 
        $this->error = mysql_error($this->conn); 
        if('' != $this->queryStr){ 
            $this->error .= "\n [ SQL语句 ] : ".$this->queryStr; 
        } 
        return $this->error; 
    } 
    /**
     +----------------------------------------------------------
     * 释放查询结果
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */ 
    public function free() { 
        @mysql_free_result($this->queryID); 
        $this->queryID = 0; 
        $this->query_list = null; 
    } 
    /**
     +----------------------------------------------------------
     * 关闭连接
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param 
     +----------------------------------------------------------
     */ 
    function close(){ 
        if ($this->conn && !mysql_close($this->conn)){ 
            throw new Exception($this->error()); 
        } 
        $this->conn = 0; 
        $this->query_count = 0; 
    } 
    /**
     +----------------------------------------------------------
     * 析构方法
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */ 
    function __destruct(){ 
         $this->close(); 
    } 

// +----------------------------------------------------------------------
// |MySQL操作类
// +----------------------------------------------------------------------
// |@微凉 QQ:496928838
// +----------------------------------------------------------------------
class MySQL{
 
 private $db_mysql_hostname;
 private $db_mysql_username;
 private $db_mysql_password;
 private $db_mysql_database;
 private $db_mysql_port;
 private $db_mysql_charset;
 
 private $query_list = array();
 
 //查询次数
 public $query_count = 0;
 //查询开始时间
 public $query_start_time;
 
 //当前查询ID
 protected $queryID;
 //当前连接
 protected $conn;
 // 事务指令数
 protected $transTimes = 0;
 // 返回或者影响记录数
    protected $numRows    = 0;
    // 错误信息
    protected $error      = '';
 
    //静态实例
    static private $db_class;
    //取得本类
    static public function getClass()
    {
      if (is_null(self::$db_class)) {
            self::$db_class=new MySQL();
        }
        return self::$db_class;
    }

    //定义为私有方法就new起来咯,要用 getClass
    private function __construct()
    {
           
    }


    public function connect($hostname_or_conf,$username,$password,$database,$port = '3306',$char = 'utf8'){
        if(is_array($hostname_or_conf)){
            $this->db_mysql_hostname = $hostname_or_conf['hostname'];
            $this->db_mysql_username = $hostname_or_conf['username'];
            $this->db_mysql_password = $hostname_or_conf['password'];
            $this->db_mysql_database = $hostname_or_conf['database'];
            $this->db_mysql_port = isset($hostname_or_conf['port'])?$hostname_or_conf['port']:'3306';
            $this->db_mysql_charset = isset($hostname_or_conf['charset'])?$hostname_or_conf['charset']:'utf8';
        }elseif(!empty($hostname_or_conf)||!empty($username)||!empty($password)||!empty($database))
        {
             $this->db_mysql_hostname = $hostname_or_conf;
             $this->db_mysql_username = $username;
             $this->db_mysql_password = $password;
             $this->db_mysql_database = $database;
             $this->db_mysql_port = $port;
             $this->db_mysql_charset = $char;
            
        }else{
            die('configuration error.');
        }
       
        $server = $this->db_mysql_hostname.':'.$this->db_mysql_port;
        $this->conn = mysql_connect($server,$this->db_mysql_username,$this->db_mysql_password,true) or die('Connect MySQL DB error!');
        mysql_select_db($this->db_mysql_database,$this->conn) or die('select db error!');
        mysql_query("set names " . $this->db_mysql_charset, $this->conn);

    }

 


 /**
     +----------------------------------------------------------
     * 设置数据对象值
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     *table,where,order,limit,data,field,join,group,having
     +----------------------------------------------------------
     */
 public function table($table){
  $this->query_list['table'] = $table;
  return $this;
 }
 
 public function where($where){
  $this->query_list['where'] = $where;
  return $this;
 }
 
 public function order($order){
  $this->query_list['order'] = $order;
  return $this;
 }
 
 public function limit($offset,$length=null){
  if (is_null($length)) {
   $this->query_list['limit']='limit '.$offset;
   return $this;
  }else {
   if(!isset($length)){
    $length = $offset;
    $offset = 0;
   }
    $this->query_list['limit'] = 'limit '.$offset.','.$length;
    return $this;
  }

 }
 
 public function data($data){
  //读取数据表字段,然后处理表单数据
  $dataList = $this->getFields($this->query_list['table']);
  $arr=array();
  foreach ($dataList as $key=>$value) {
   if (array_key_exists ($key,$data) ) {
    $arr[$key]=$data[$key];
   }
   
  }
  //var_dump($arr);
  /*
  if(is_object($data)){
   $data   =   get_object_vars($data);
  }elseif (is_string($data)){
   parse_str($data,$data);
  }elseif(!is_array($data)){
   //Log:DATA_TYPE_INVALID
  }
  */
  $this->query_list['data'] = $arr;
  return $this;
 }
 public function field($fields){
  $this->query_list['fields'] = $fields;
  return $this;
 }
 public function join($join){
  $this->query_list['join'] = $join;
  return $this;
 }
 public function group($group){
  $this->query_list['group'] = $group;
  return $this;
 }
 public function having($having){
  $this->query_list['having'] = $having;
  return $this;
 }
 /**
     +----------------------------------------------------------
     * 查询
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param
     +----------------------------------------------------------
     */
 public function select(){
  $select_sql = 'select ';
  $fields = isset($this->query_list['fields'])?$this->query_list['fields']:'*';
  $select_sql.=$fields;
  $select_sql.= ' from `'.$this->query_list['table'].'` ';
  
  isset($this->query_list['join'])?($select_sql.=$this->query_list['join']):'';
  isset($this->query_list['where'])?($select_sql.=' where '.$this->query_list['where']):'';
  isset($this->query_list['group'])?($select_sql.=' group by'.$this->query_list['group']):'';
  isset($this->query_list['having'])?($select_sql.=' mysql having '.$this->query_list['having']):'';
  isset($this->query_list['order'])?($select_sql.=' order by '.$this->query_list['order']):'';
  isset($this->query_list['limit'])?($select_sql.=' '.$this->query_list['limit']):'';
 // echo '----->'.$select_sql;
  return $this->query($select_sql);
 }
 /**
     +----------------------------------------------------------
     * 增加
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param
     +----------------------------------------------------------
     */
 public function add(){
  $add_sql = 'insert into `'.$this->query_list['table'].'` (';
  
  $data = $this->query_list['data'];
  $value = $field = '';
  foreach($data as $k=>$v){
   $field .= '`'.$k.'`,';
   if(is_numeric($v))
    $value .= $v.',';
   else
    $value .= '\''.$v.'\',';
  }
  $add_sql .= rtrim($field,',').') values ('.rtrim($value,',').')';

 // echo 'add_sql'.$add_sql;
  return $this->execute($add_sql);
 }
 /**
     +----------------------------------------------------------
     * 删除
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param
     +----------------------------------------------------------
     */
 public function delete(){
  $del_sql = 'delete from `'.$this->query_list['table'].'` where '.$this->query_list['where'];
  
  if(isset($this->query_list['order']))
   $del_sql .= 'order by '.$this->query_list['order'];
  if(isset($this->query_list['limit']))
   $del_sql .= ' '.$this->query_list['limit'];
   
  return $this->execute($del_sql);
  
 }
 /**
     +----------------------------------------------------------
     * 更新
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param
     +----------------------------------------------------------
     */
 public function update(){
  $update_sql = 'update `'.$this->query_list['table'].'` set ';
  $data = $this->query_list['data'];
  
  foreach($data as $k=>$v){
   if(is_numeric($v))
    $update_sql .= '`'.$k.'` ='.$v.',';
   else
    $update_sql .= '`'.$k.'` =\''.$v.'\',';
  }
  $update_sql = rtrim($update_sql,',');
  if(isset($this->query_list['where']))
   $update_sql .= ' where '.$this->query_list['where'];
  if(isset($this->query_list['order']))
   $update_sql .= ' order by '.$this->query_list['order'];
  if(isset($this->query_list['limit']))
   $update_sql .= ' '.$this->query_list['limit'];
  
  return $this->execute($update_sql);
  
 }
  /**
     +----------------------------------------------------------
     * 执行查询 返回数据集
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $sql  sql指令
     */
    public function query($sql) {
        if ( !$this->conn ) return false;
        $this->queryStr = $sql;
        //释放前次的查询结果
        if ( $this->queryID ) {    $this->free();    }
       
        $this->query_start_time = microtime(true);
       
        $this->queryID = mysql_query($sql, $this->conn);
        $this->query_count++;
        if ( false === $this->queryID ) {
            $this->error();
            return false;
        } else {
            $this->numRows = mysql_num_rows($this->queryID);
            return $this->getAll();
        }
    }
 /**
     +----------------------------------------------------------
     * 执行语句
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $sql  sql指令
     +----------------------------------------------------------
     */
    public function execute($sql) {
        if ( !$this->conn ) return false;
        $this->queryStr = $sql;
        //释放前次的查询结果
        if ( $this->queryID ) {    $this->free();    }
       
        $this->query_start_time = microtime(true);
       
        $result =   mysql_query($sql, $this->conn) ;
        $this->query_count++;
        if ( false === $result) {
            $this->error();
            return false;
        } else {
            $this->numRows = mysql_affected_rows($this->conn);
            return $this->numRows;
        }
    }
 /**
     +----------------------------------------------------------
     * 获得所有的查询数据
     +----------------------------------------------------------
     * @access private
     +----------------------------------------------------------
     * @return array
     */
    private function getAll() {
        //返回数据集
        $result = array();
        if($this->numRows >0) {
            while($row = mysql_fetch_assoc($this->queryID)){
                $result[]   =   $row;
            }
            mysql_data_seek($this->queryID,0);
        }
        return $result;
    }
 /**
     +----------------------------------------------------------
     * 取得数据表的字段信息
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    public function getFields($tableName) {
        $result =   $this->query('SHOW COLUMNS FROM `'

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Meilleurs paramètres graphiques
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Comment réparer l'audio si vous n'entendez personne
3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Où trouver la courte de la grue à atomide atomique
1 Il y a quelques semaines By DDD

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Configuration du projet CakePHP Configuration du projet CakePHP Sep 10, 2024 pm 05:25 PM

Dans ce chapitre, nous comprendrons les variables d'environnement, la configuration générale, la configuration de la base de données et la configuration de la messagerie dans CakePHP.

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 apporte plusieurs nouvelles fonctionnalités, améliorations de sécurité et de performances avec une bonne quantité de dépréciations et de suppressions de fonctionnalités. Ce guide explique comment installer PHP 8.4 ou mettre à niveau vers PHP 8.4 sur Ubuntu, Debian ou leurs dérivés. Bien qu'il soit possible de compiler PHP à partir des sources, son installation à partir d'un référentiel APT comme expliqué ci-dessous est souvent plus rapide et plus sécurisée car ces référentiels fourniront les dernières corrections de bogues et mises à jour de sécurité à l'avenir.

Date et heure de CakePHP Date et heure de CakePHP Sep 10, 2024 pm 05:27 PM

Pour travailler avec la date et l'heure dans cakephp4, nous allons utiliser la classe FrozenTime disponible.

CakePHP travaillant avec la base de données CakePHP travaillant avec la base de données Sep 10, 2024 pm 05:25 PM

Travailler avec la base de données dans CakePHP est très simple. Nous comprendrons les opérations CRUD (Créer, Lire, Mettre à jour, Supprimer) dans ce chapitre.

Téléchargement de fichiers CakePHP Téléchargement de fichiers CakePHP Sep 10, 2024 pm 05:27 PM

Pour travailler sur le téléchargement de fichiers, nous allons utiliser l'assistant de formulaire. Voici un exemple de téléchargement de fichiers.

Routage CakePHP Routage CakePHP Sep 10, 2024 pm 05:25 PM

Dans ce chapitre, nous allons apprendre les sujets suivants liés au routage ?

Discuter de CakePHP Discuter de CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP est un framework open source pour PHP. Il vise à faciliter grandement le développement, le déploiement et la maintenance d'applications. CakePHP est basé sur une architecture de type MVC à la fois puissante et facile à appréhender. Modèles, vues et contrôleurs gu

CakePHP créant des validateurs CakePHP créant des validateurs Sep 10, 2024 pm 05:26 PM

Le validateur peut être créé en ajoutant les deux lignes suivantes dans le contrôleur.

See all articles