欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

关于类的$this的有关问题

程序员文章站 2022-05-23 14:56:48
...
关于类的$this的问题
PHP code

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']),
关于类的$this的有关问题

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频