Simple MySQLDB class, simple MySQLDB class

Source: Internet
Author: User

Simple MySQLDB class, simple MySQLDB class
1 <? Php 2 header ('content-Type: text/html; charset = UTF-8 '); 3 error_reporting (E_ALL ^ E_DEPRECATED ); 4 // database operation class 5 class MySQLDB {6 7 // Attributes -- required information 8 private $ _ host; // host 9 private $ _ port; // port 10 private $ _ user; // username 11 private $ _ pass; // password 12 private $ _ charset; // character set 13 private $ _ dbname; // default database 14 15 // attribute -- 16 public $ _ link is generated during runtime; // mysql link resource 17 18 private static $ _ instance; 19 public static function GetInstance ($ params = array () {20 if (! Self: $ _ instance instanceof self) {21 self ::: _ instance = new self ($ params); 22} 23 return self ::$ _ instance; 24} 25 private function _ clone () {} 26 27/* 28 * constructor 29 * @ param $ param = array () array associated array requires six attribute values 30 */31 private function _ construct ($ params = array ()) {32 // initialization property 33 $ this-> _ initParams ($ params); 34 // connect to the database server 35 $ this-> _ connect (); 36 // set Character Set 37 $ this-> _ setCharset (); 38 // select the default data 39 $ thi S-> _ selectDB (); 40} 41 42/* 43 * initialize Database Server parameter 44 */45 private function _ initParams ($ params) {46 $ this-> _ host = isset ($ params ['host'])? $ Params ['host']: '192. 0.0.1 '; // Local 47 $ this-> _ port = isset ($ params ['Port'])? $ Params ['Port']: '000000'; // 3306 48 $ this-> _ user = isset ($ params ['user'])? $ Params ['user']: ''; // anonymous user 49 $ this-> _ pass = isset ($ params ['pass'])? $ Params ['pass']: ''; // No Password 50 $ this-> _ charset = isset ($ params ['charset'])? $ Params ['charset']: 'utf8'; // utf8 51 $ this-> _ dbname = isset ($ params ['dbname'])? $ Params ['dbname']: ''; // indicates you do not need to select the default database 52} 53 54 55/* 56 * connect to the database server 57 */58 private function _ connect () {59 // 127.0.0.1: 3306 60 if ($ link = mysql_connect ("$ this-> _ host: $ this-> _ port", $ this-> _ user, $ this-> _ pass) {61 // success 62 $ this-> _ link = $ link; 63} else {64 // failed, force the script to end, error message 65 die ("-_-!, Failed to connect to the database server. Please confirm the connection option "); 66} 67} 68 69/* 70 * set the specified character set 71 */72 private function _ setCharset () {73 $ SQL = "set names $ this-> _ charset"; 74 if (mysql_query ($ SQL, $ this-> _ link )) {75 // success 76} else {77 // execution failure error message 78 echo '-_-! SQL Execution failed <br/> '; 79 echo': ', $ SQL,' <br/> '; 80 echo :', mysql_errno ($ this-> _ link), '<br/>'; 81 echo 'error message:', mysql_error ($ this-> _ link); 82 die; 83} 84} 85 86/* 87 * select default database 88 * if not specified, then no 89 */90 private function _ selectDB () is selected () {91 // determine whether to select the default database, indicating no, to prevent it from being 0 92 if ($ this-> _ dbname! = '') {93 // if not empty, select database 94 $ SQL =" use '$ this-> _ dbname' "; 95 if (mysql_query ($ SQL, $ this-> _ link) {96 // success 97} else {98 // execution failure error message 99 echo '-_-! SQL Execution failed <br/> '; 100 echo': ', $ SQL,' <br/> '; 101 echo :', mysql_errno ($ this-> _ link), '<br/>'; 102 echo 'error message:', mysql_error ($ this-> _ link); 103 die; 104} 105} 106} 107 108 109 110/** 111 * method executed during serialization 112 * @ return array each array element is a property name to be serialized 113*114*/115 public function _ sleep () {116 return array ('_ host',' _ Post', '_ user',' _ pass', '_ charset', '_ dbname '); 117} 118 119/* 120 * deserialization is executed 121 * reinitialization 122 */123 public function _ wakeup () {124 // connect to the database server 125 $ this-> _ connect (); 126 // set the connection Character Set 127 $ this-> _ setCharset (); 128 // select the default database 129 $ this-> _ selectDB (); 130} 131}View Code

 


Simple problem about Data Types in mysql

This field is an integer with a length of 10 characters.

Mysql: A simple question about creating a table

Obviously wrong. There is no such data type as, and there is still a last item without a comma

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.