无意义
这里的东西都是没什么意义的,个人练习的一些东西。
<?php namespace database; function p($arr, $p=null) { echo '<pre>'."\n"; if($p==null) { print_r($arr); }else{ var_dump($arr); } echo '</pre>'; } class Mysql_db { private static $instance = null; private static $hostname = 'localhost'; private static $username = 'root'; private static $password = 'root'; private static $database = ''; protected $columns = [];// the columns of specific table private $client_info = null; private $server_info = null; private $server_version = null; private $client_version = null; private $last_insert_id = null; private $mysqli = null; // initialize data parameters public function __construct($hostname, $username, $password, $database) { self::$hostname = $hostname; self::$username = $username; self::$password = $password; self::$database = $database; // 连接数据库 $this->mysqli = $this->connect(); $this->client_version = mysqli_get_client_version(); $this->client_info = mysqli_get_client_info(); $this->server_version = $this->_get_server_version(); $this->server_info = $this->_get_server_info(); } public function __get($name) { return $this->$name; } private function _error_msg($msg) { return "[".date("Y-m-d H:i:s")."] ".$msg."\n"; } private function _get_server_version() { return $this->mysqli->server_version; } private function _get_server_info() { return $this->mysqli->server_info; } // database connection public function connect() { $mysqli = @new \mysqli(self::$hostname, self::$username, self::$password, self::$database); if($mysqli->connect_error) { $e_msg = $this->_error_msg('Database Connection Error, '.$mysqli->connect_errno.': '.$mysqli->connect_error); die($e_msg); } return $mysqli; } // create database public function create_database($dbname) { if( !$dbname ) { $e_msg = "Database name is required"; die($e_msg); } $sql = "create database ".'`'.$dbname.'`'; if($this->mysqli->query($sql)) { $s_msg = 'Database created successfully'; return $s_msg; } else { $e_msg = $this->_error_msg('Error creating database: '.$this->mysqli->error); return $e_msg; } } // drop database public function drop_database($dbname) { if( !$dbname ) { $e_msg = "Database name is required"; die($e_msg); } $sql = "drop database `{$dbname}`"; if($this->mysqli->query($sql)) { $s_msg = "Database dropped successfully"; return $s_msg; } else { $e_msg = $this->get_error_msg('Error dropping database'); return $e_msg; } } // create table public function create_table($create_sql) { if($create_sql) { if( !$this->mysqli->query($create_sql) ) { die("Error creating table: ".$create_sql); } else { echo "Table created successfully"; } } else { return false; } } // change the storage engine of an existing table public function change_engine($table_name,$engine_type) { $sql = "alter table `{$table_name}` engine=`{$engine_type}`"; if( $this->mysqli->query($sql) ) { echo 'Table Engine changed successfully'; } else { $e_msg = "Error changing table($table_name) engine"; die($e_msg); } } // show status public function show_status($table_name, $specific_columns=null) { $info_columns = [ 'TABLE_CATALOG', 'TABLE_SCHEMA', 'TABLE_NAME', 'TABLE_TYPE', 'ENGINE', 'VERSION', 'ROW_FORMAT', 'TABLE_ROWS', 'AVG_ROW_LENGTH', 'DATA_LENGTH', 'MAX_DATA_LENGTH', 'INDEX_LENGTH', 'DATA_FREE', 'AUTO_INCREMENT', 'CREATE_TIME', 'UPDATE_TIME', 'CHECK_TIME', 'TABLE_COLLATION', 'CHECKSUM', 'CREATE_OPTIONS', 'TABLE_COMMENT' ]; if($specific_columns==null) { $info_columns = implode(',', $info_columns); } else { if(is_array($specific_columns)) { $info_columns = implode(',', $info_columns); } else { $info_columns = $info_columns; } } $sql = "select {$info_columns} from information_schema.tables where table_schema=DATABASE() and table_name='{$table_name}'"; if($results = $this->mysqli->query($sql)) { // return arrays $row = $results->fetch_assoc(); $results->free(); } else { return false; } return $row; } // insert data public function insert($table_name, $keys, $values) { // sql: insert into(name1,name2,name3) values($value1,$value2,$value3),($value11,$value22,$value33); $sql = "insert into `{$table_name}`(".implode(',', $keys).") values"; // if $batch == true; $rows = null; if(count($keys) === count($values[0])) { $rows = $values; } else { $rows[] = $values; } $insert_data = implode(',',array_map(function($x){ return '(\''.implode("','",$x).'\')'; },$rows)); $sql .= $insert_data; if($result = $this->mysqli->query($sql)) { $first_insert_id=$this->mysqli->insert_id; $affected_rows = $this->mysqli->affected_rows; return $this->last_insert_id = $first_insert_id+$affected_rows-1; } else { return $result; } } // get columns from table public function get_columns($tablename, $detailed=null) { $sql = 'desc '."`{$tablename}`"; if($result = $this->mysqli->query($sql)) { $fields = []; while($field = $result->fetch_assoc()) { $fields[] = $field; } } else { return false; } if($detailed==null) { // return array_map(function($arr){ // return $arr['Field']; // },$fields); // OR return array_column($fields, 'Field'); } else { return $fields; } } // singleton pattern public static function get_instance() { if( !(self::$instance instanceof Mysql_db) ) { self::$instance = new self(self::$hostname, self::$username, self::$password, self::$database); } return self::$instance; } // destruct public function __destruct() { $this->mysqli->close(); } } // $m = new Mysql_db('localhost', 'root', 'root', 'ecshop'); // // create database; // echo $m->create_database('z'); // drop database; // echo $m->drop_database('z'); // // create table; // $create_sql = <<<CREATE_A // create table `a`( // `id` int unsigned not null primary key auto_increment, // `name` char(30) not null default '', // `pwd` char(30) not null default '', // `age` int(3) unsigned not null default 0 // )engine=innodb default charset=utf8; // CREATE_A; // $m->create_table($create_sql); // // change engine type // $m->change_engine('a', 'myisam'); // show specific table status // $info = $m->show_status('a'); // p($info); // get columns from specific table // $columns = $m->get_columns('a'); // p($columns); // // get client info // $client_info = $m->client_info; // p($client_info); // $server_info = $m->server_info; // p($server_info); // // server version // p($m->server_version); // p($m->client_version); // $m->age = 7; // p($m->age); // insert data to specific table // --------------------------------- // $r = $m->insert( // 'a', // ['name','pwd','age'], // [ // ['Batch1', '111', 14], // ['Batch2', '233', 24], // ['Batch3', '355', 34], // ['Batch4', '466', 44], // ['Batch5', '588', 54], // ['Batch6', '699', 64], // ] // ); // p($r, 1); // p($m->last_insert_id,1); // ----------------------------------- // 工厂模式:一个新类--工厂类,这个类中的某个静态方法“负责”创建类 class Factory {// 这是一个工厂类 // 生成mysqli_db对象 public static function create_Mysql_db($hostname, $username, $password, $database) { // // 工厂模式 // $db = new \database\Mysql_db($hostname, $username, $password, $database); // return $db; // 工厂模式和单例模式结合 $db = new \database\Mysql_db($hostname, $username, $password, $database); return $db::get_instance(); } } $db = Factory::create_Mysql_db('localhost', 'root', 'root', 'ecshop'); // $columns = $db->get_columns('a', 1); // p($columns); // $r = $db->insert( // 'a', ['name', 'pwd', 'age'], // [ // ['Zell', 'fuc', 30] // ] // ); // p($r, 1);
---- 始终相信这句:
----“做每天该做的事,不计结果!”
---- 因爲對於編程還只是新手,對很多知識掌握的不牢靠,歡迎大家批評指正~~|=-=|~~
----“做每天该做的事,不计结果!”
---- 因爲對於編程還只是新手,對很多知識掌握的不牢靠,歡迎大家批評指正~~|=-=|~~