>백엔드 개발 >PHP 튜토리얼 >关于类的$this的有关问题

关于类的$this的有关问题

WBOY
WBOY원래의
2016-06-13 10:52:25927검색

关于类的$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']),
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.