Rumah > pembangunan bahagian belakang > tutorial php > 关于类的$this的有关问题

关于类的$this的有关问题

WBOY
Lepaskan: 2016-06-13 13:10:16
asal
877 orang telah melayarinya

关于类的$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;
            }
        }

Salin selepas log masuk



为什么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']),
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan