首页 > 后端开发 > php教程 > 关于类的$this的有关问题

关于类的$this的有关问题

WBOY
发布: 2016-06-13 13:10:16
原创
874 人浏览过

关于类的$this的问题

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
class User {
   private $uid;        
   private $fields;    

   public function __construct() {
            $this->uid = null;
            $this->fields = array(
                            'username' => '',
                            'password' => '',
                            'emailAddr' => '',
                            'isActive' => false,
                            'permission' => 0
                         );
   }

   public static function getById($user_id) {
            $user = new User();
            
            $query = sprintf('SELECT username, password, email_addr, is_active, permission FROM %suser WHERE user_id = %d',
                                DB_TBL_PREFIX, $user_id);
            $result = mysql_query($query, $GLOBALS['DB']);
            if (mysql_num_rows($result)) {
                $row = mysql_fetch_assoc($result);
                
                $user->username = $row['username'];
                $user->password = $row['password'];
                $user->emailAddr = $row['email_addr'];
                $user->isActive = $row['is_active'];
                $user->permission = $row['permission'];
                $user->uid = $user_id;
            }
            mysql_free_result($result);
            
            return $user;
        }
   }

   public function save() {
            if ($this->uid) {
                $query = sprintf('UPDATE %suser SET username = "%s", password = "%s", email_addr = "%s", 
                                        is_active = %d, permission = %d WHERE user_id = %d',
                                                        DB_TBL_PREFIX,
                                                        mysql_real_escape_string($this->username, $GLOBALS['DB']),
                                                        mysql_real_escape_string($this->password, $GLOBALS['DB']),
                                                        mysql_real_escape_string($this->emailAddr, $GLOBALS['DB']),
                                                        $this->isActive,
                                                        $this->permission, 
                                                        $this->uid);
                return mysql_query($query, $GLOBALS['DB']);
            }
            else {
                $query = sprintf('INSERT INTO %suser(username, password, email_addr, is_active, permission) 
                                                                                VALUES("%s", "%s", "%s", %d, %d)',
                                                        DB_TBL_PREFIX,
                                                        mysql_real_escape_string($this->username, $GLOBALS['DB']),
                                                        mysql_real_escape_string($this->password, $GLOBALS['DB']),
                                                        mysql_real_escape_string($this->emailAddr, $GLOBALS['DB']),
                                                        $this->isActive,
                                                        $this->permission);
                if (mysql_query($query, $GLOBALS['DB'])) {
                    $this->uid = mysql_insert_id($GLOBALS['DB']);
                    return true;
                }
                else
                    return false;
            }
        }

登录后复制



为什么save()中的
$query = sprintf('UPDATE %suser SET username = "%s", password = "%s", email_addr = "%s", 
is_active = %d, permission = %d WHERE user_id = %d',
DB_TBL_PREFIX,
mysql_real_escape_string($this->username, $GLOBALS['DB']),
mysql_real_escape_string($this->password, $GLOBALS['DB']),
mysql_real_escape_string($this->emailAddr, $GLOBALS['DB']),
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板