Encapsulation, mysql class

Source: Internet
Author: User
No details about MySQL? PhpclassMysql {private $ host; private $ user; private $ pwd; private $ dbName; private $ charset; private $ connnull; publicfunction _ construct () {$ this-hostlocalhost; $ this-userroot; $ this-pwdroot; $ this-dbName

No details about MySQL? Php class Mysql {private $ host; private $ user; private $ pwd; private $ dbName; private $ charset; private $ conn = null; public function _ construct () {$ this-host = 'localhost'; $ this-user = 'root'; $ this-pwd = 'root'; $ this-dbName =

<无详细内容> MySQL
  Host = 'localhost'; $ this-> user = 'root'; $ this-> pwd = 'root'; $ this-> dbName = 'test '; $ this-> connect ($ this-> host, $ this-> user, $ this-> pwd); $ this-> switchDb ($ this-> dbName ); $ this-> setChar ($ this-> charset);} // link to private function connect ($ h, $ u, $ p) {$ conn = mysql_connect ($ h, $ u, $ p); $ this-> conn = $ conn;} // switch the public function switchDb ($ db) {$ SQL = 'use '. $ db; $ this-> query ($ SQL);} // sets the character set public function setCh. Ar ($ char) {$ SQL = 'set names '. $ char; $ this-> query ($ SQL);} // sends an SQL query public function query ($ SQL) {return mysql_query ($ SQL, $ this-> conn);} // obtains the select result of multiple rows and multiple columns. public function getAll ($ SQL) {$ list = array (); $ rs = $ this-> query ($ SQL); if (! $ Rs) {return false;} while ($ row = mysql_fetch_assoc ($ rs) {$ list [] = $ row;} return $ list ;} public function getRow ($ SQL) {$ rs = $ this-> query ($ SQL); if (! $ Rs) {return false;} return mysql_fetch_assoc ($ rs);} public function getOne ($ SQL) {$ rs = $ this-> query ($ SQL); if (! $ Rs) {return false;} return mysql_fetch_assoc ($ rs); return $ row [0];} public function close () {mysql_close ($ this-> conn );}} echo'
'; $ Mysql = new Mysql (); print_r ($ mysql); $ SQL = "insert into stu values (4, 'wangwu', '123 ')"; if ($ mysql-> query ($ SQL) {echo "query succeeded";} else {echo "failed";} echo"
"; $ SQL =" select * from stu "; $ arr = $ mysql-> getAll ($ SQL); print_r ($ arr);?>
Related Article

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.