Home  >  Article  >  Backend Development  >  Simple MySQLDB class, simple MySQLDB class_PHP tutorial

Simple MySQLDB class, simple MySQLDB class_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:43877browse

Simple MySQLDB class, Simple MySQLDB class

Simple MySQLDB class, simple MySQLDB class_PHP tutorial 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--Necessary information 8 private $_host; //host 9 private $_port; //port 10 private $_user; //username 11 private $_pass; //Password 12 private $_charset;//Charset 13 private $_dbname; //Default database 14 15 //Properties--runtime generation 16 public $_link; //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 * Construction method 29 * @param $param=array() array associative array requires six attribute values 30 */ 31 private function __construct($params=array()){ 32 //Initialization properties 33 $this->_initParams($params); 34 //Connect to the database server 35 $this->_connect(); 36 //Set character set 37 $this->_setCharset(); 38 //Select default data 39 $this->_selectDB(); 40 } 41 42 /* 43 * Initialize database server parameters 44 */ 45 private function _initParams($params){ 46 $this->_host = isset($params['host'])?$ params['host']:'127.0.0.1'; //Native 47 $this->_port = isset($params['port'])?$params['port']:'3306'; //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']:''; // means there is no need to select the default database 52 } 53 54 55 /* 56 * Connect to 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, forcing the script to end and prompting an error message 65 die("-_-!, Failed to connect to the database server, please confirm the connection options"); 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 fails and error message is given 78 echo '-_-!SQL execution failed
'; 79 echo 'The wrong SQL is:',$sql,'
'; 80 echo 'Error code is:',mysql_errno($this->_link),'
'; 81 echo 'The error message is:',mysql_error($this->_link); 82 die; 83 } 84 } 85 86 /* 87 * Select default database 88 * If not specified, not selected 89 */ 90 private function _selectDB(){ 91 // Determine whether the default database needs to be selected, indicating no need, prevent it from being 0 92 if($this->_dbname !== ''){ 93 // is not empty, then select the database 94 $sql = "use `$this->_dbname`"; 95 if(mysql_query($sql,$this->_link )){ 96 //Success 97 }else{ 98 //Execution fails and error message is given 99 echo '-_-!SQL execution failed
'; 100 echo 'The wrong SQL is:',$sql,'
'; 101 echo 'The error code is:',mysql_errno($this->_link),'
'; 102 echo 'The error message is:',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 an attribute name that needs to be serialized 113 * 114 */ 115 public function __sleep(){ 116 return array('_host','_post','_user','_pass','_charset','_dbname'); 117 }118 119 /* 120 * Executed during deserialization 121 * Re-initialization work 122 */ 123 public function __wakeup(){ 124 //Connect to database server 125 $this->_connect(); 126 //Set connection character set 127 $this->_setCharset(); 128 //Select default database 129 $this->_selectDB(); 130 } 131 } View Code

Simple question about data types in mysql

That is to say, this field is an integer and the length is 10 characters.

mysql, simple problem of creating table

Obviously wrong, there is no such data type as 1, 2, and the last item does not need a comma

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/903172.htmlTechArticleSimple 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 MySQL...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn