⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class.widget.php

📁 Professional PHP5 code for this book
💻 PHP
字号:
<?phpclass Widget {    private $id;  private $name;  private $description;  private $hDB;  private $needsUpdating = false;    public function __construct($widgetID) {    //The widgetID parameter is the primary key of a     //record in the database containing the information    //for this object    //Create a connection handle and store it in a private member variable    $this->hDB = pg_connect('dbname=parts user=postgres');    if(! is_resource($this->hDB)) {      throw new Exception('Unable to connect to the database.');    }        $sql = "SELECT \"name\", \"description\" FROM widget WHERE widgetid = $widgetID";    $rs = pg_query($this->hDB, $sql);    if(! is_resource($rs)) {      throw new Exception("An error occurred selecting from the database.");    }        if(! pg_num_rows($rs)) {      throw new Exception('The specified widget does not exist!');    }            $data = pg_fetch_array($rs);    $this->id = $widgetID;    $this->name = $data['name'];    $this->description = $data['description'];      }    public function getName() {    return $this->name;  }    public function getDescription() {    return $this->description;  }    public function setName($name) {    $this->name = $name;    $this->needsUpdating = true;  }    public function setDescription($description) {    $this->description = $description;    $this->needsUpdating = true;  }    public function __destruct() {    if(! $this->needsUpdating) {      return;    }        $sql = 'UPDATE "widget" SET ';    $sql .= "\"name\" = '" . pg_escape_string($this->name) . "', ";    $sql .= "\"description\" = '" . pg_escape_string($this->description) . "' ";    $sql .= "WHERE widgetID = " . $this->id;        $rs = pg_query($this->hDB, $sql);    if(! is_resource($rs)) {      throw new Exception('An error occurred updating the database');    }        //You're done with the database.  Close the connection handle.    pg_close($this->hDB);  }}?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -