PHP での mysql モジュールの一部の関数の単純なカプセル化

高洛峰
リリース: 2023-03-01 13:10:02
オリジナル
1040 人が閲覧しました

コードは次のとおりです:

class mysql { private $db; // datebase connect private $result; // mysql result static private $mysql; // mysql object private function __construct() { // The work before Create an object $this->db = mysql_connect('localhost','root',''); mysql_select_db('hello', $this->db ); } public static function getObject() { //if have a object,return that object,Not create if(! self::$mysql instanceof self) self::$mysql = new self; return self::$mysql; } public function query($sql) { $this->result = mysql_query($sql, $this->db); return $this->result; } public function fetch() { if( isset($this->result ) ) return mysql_fetch_assoc( $this->result ); } public function error() { return 'error:'.mysql_error(); } public function num() // for sql select result { return mysql_num_rows( $this->result ); } public function close() { // return true or false return mysql_close( $this->db ); } }
ログイン後にコピー


関連ラベル:
php
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!