PHP Code display:
1<?PHP2 //class name, also customary (recommended) to use a name similar to the file name3 //defines a class that can connect to a MySQL database4 //and returns the resource (or the failure is terminated) after the connection5 classmysqldb{6 Public$host;7 Public$port;8 Public$username;9 Public$password;Ten Public$charset; One Public$dbname; A - //Connection Results (resources) - Static$link; the - //constructor Function - Publicfunction __construct ($config) { - //Initializing Data +$ This->host = Isset ($config ['Host']) ? $config ['Host'] :'localhost'; -$ This->port = Isset ($config ['Port']) ? $config ['Port'] :'3306'; +$ This->username = Isset ($config ['username']) ? $config ['username'] :'Root'; A$ This->password = Isset ($config ['Password']) ? $config ['Password'] :"'; at$ This->charset = Isset ($config ['CharSet']) ? $config ['CharSet'] :'UTF8'; -$ This->dbname = Isset ($config ['dbname']) ? $config ['dbname'] :"'; - - //connecting to a database -Self:: $link = $ This-Connect (); - //Setting the connection code in$ This->setcharset ($ This-charset); - //Selected Database to$ This->selectdb ($ This-dbname); + } - //here to connect the Publicfunction Connect () { *$link = mysql_connect ("$this->host: $this->port","$this->username","$this->password") or Die ("failed to connect to database! "); $ return$link;Panax Notoginseng } - Publicfunction Setcharset ($charset) { the Mysql_set_charset ($charset, Self:: $link); + } A Publicfunction Selectdb ($dbname) { the mysql_select_db ($dbname, Self:: $link) + } - } $ $ //First Imagine: -$config =Array ( - 'Host'='localhost', the 'Port'='3306', - 'username'='Root',Wuyi 'Password'='123', the 'CharSet'='UTF8', - 'dbname'='php34', Wu ); -$link =NewMySQLdb ($config); About$result = $link->query ("Delete from tab1 where id=1");
That's probably it, I didn't try, but that's the way of thinking.
PHP, database connection class