PHP, database connection class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.