Simple MySQLdb class, Simple MySQLdb class _php tutorial

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 Operations Classes 5 class mysqldb{ 6 7 // Properties--Necessary information 8 Private $_host; // Host 9 Private $_port; // Port Ten Private $_user; // User name One Private $_pass; // Password A Private $_charset; // Character Set - Private $_dbname; // default Database - the //Properties--run-time generation - Public $_link; // MySQL Link resource - - Private Static $_instance ; + Public Static functionGetInstance ( $params= Array ()){ - if(!self:: $_instance instanceof Self) { +Self:: $_instance= NewSelf $params ); A } at returnSelf:: $_instance ; - } - Private function __clone () {} - - /* - * Construction Method in * @param $param =array () array associative array requires six property values - */ to Private function__construct ( $params= Array ()){ + // Initialize Properties - $this->_initparams ( $params ); the // connecting to the database server * $this- _connect (); $ // Set character set Panax Notoginseng $this- _setcharset (); - // Select default Data the $this- _selectdb (); + } A the /* + * Initialize database server parameters - */ $ Private function_initparams ( $params ){ $ $this->_host = isset( $params[' Host ']? $params[' Host ']: ' 127.0.0.1 '; // This machine - $this->_port = isset( $params[' Port ']? $params[' Port ']: ' 3306 '; // 3306 - $this->_user = isset( $params[' User ']? $params[' User ']: '; // Anonymous User the $this->_pass = isset( $params[' Pass '])? $params[' Pass ']: '; // No password - $this->_charset = isset( $params[' CharSet '])? $params[' CharSet ']: ' UTF8 '; // UTF8 Wuyi $this->_dbname = isset( $params[' dbname '])? $params[' dbname ']: '; // indicates that the default database is not selected the } - Wu - /* About * Connect to database server $ */ - Private function _connect () { - // 127.0.0.1:3306 - if( $link= mysql_connect(" $this->_host: $this->_port ", $this->_user, $this- _pass)) { A // Success + $this->_link = $link ; the} Else { - // failed, force script to end, prompt error message $ die("-_-!, failed to connect to the database server, please determine connection options" ); the } the } the the /* - * Set the specified character set in */ the Private function _setcharset () { the $sql= "Set names $this->_charset " ; About if( mysql_query( $sql, $this- _link)) { the // Success the} Else { the // execution failure gives an error message + Echo'-_-! SQL execution failed
' ; - Echo' The wrong SQL is: ', $sql,'
' ; the EchoThe error code is: ', Mysql_errno( $this->_link), '
' ; Bayi Echo' The error message is: ', Mysql_error( $this- _link); the die ; the } - } - the /* the * Select default database the * If not specified, do not select the */ - Private function _selectdb () { the // determine if you need to select a default database, which indicates no need to prevent 0 the if( $this->_dbname!== ' ){ the // not empty, select database 94 $sql= "Use" $this->_dbname ' " ; the if( mysql_query( $sql, $this- _link)) { the // Success the} Else { 98 // execution failure gives an error message About Echo'-_-! SQL execution failed
' ; - Echo' The wrong SQL is: ', $sql,'
' ; 101 EchoThe error code is: ', Mysql_errno( $this->_link), '
' ; 102 Echo' The error message is: ', Mysql_error( $this- _link); 103 die ; 104 } the } 106 } 107 108 109 the /* * 111 * methods to be executed when serializing the * @return Array Each array element is a property name that needs to be serialized 113 * the */ the Public function __sleep () { the return Array(' _host ', ' _post ', ' _user ', ' _pass ', ' _charset ', ' _dbname ' ); 117 } 118 119 /* - * is executed when deserializing 121 * Re-initialization of work 122 */ 123 Public function __wakeup () { 124 // connecting to the database server the $this- _connect (); 126 // Setting the connection character set 127 $this- _setcharset (); - // Select the default database 129 $this- _selectdb (); the } 131} View Code


Simple problems with data types in MySQL

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

MySQL, simple problem of creating tables

Obviously wrong, No 1, 2, such data type, and the last item without commas

http://www.bkjia.com/PHPjc/903172.html www.bkjia.com true http://www.bkjia.com/PHPjc/903172.html techarticle simple MySQLdb class, Simple MySQLdb Class 1? PHP 2 header (' content-type:text/html; Charset=utf-8 '); 3 error_reporting (e_all ^ e_depr ecated); 4//Database Operations 5 class MySQL ...

  • 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.