Heim > Backend-Entwicklung > PHP-Tutorial > 关于类的$this的有关问题

关于类的$this的有关问题

WBOY
Freigeben: 2016-06-13 13:10:16
Original
875 Leute haben es durchsucht

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

Nach dem Login kopieren



为什么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']),
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage