By yourself, you can write a MySQL database operation class with simple functions.
<? Php/*** Created by JetBrains PhpStorm. * User: JAE * Date: 13-8-13 * Time: * Blog: http://blog.jaekj.com * QQ: 734708094 * General database operation class * version: v1.1 * // * Database Configuration return array ('db _ config' => array (// Database Configuration 'db _ host' => '2017. 0.0.1 ', // server address 'db _ name' => 'tmp', // database NAME 'db _ user' => 'root ', // username 'db _ pwd' => '', // password 'db _ encoding' => 'utf8 ', // encode 'db _ prefix' => 'dmt_' // database table PREFIX); */class M {private $ link ;/ /Database connection private $ table; // table name private $ prefix; // table prefix private $ db_config; // database configuration/*** parameter: table Name: Database Configuration array or database configuration file path * @ param $ table * @ param string $ db_config_arr_path */function _ construct ($ table, $ db_config_arr_path = 'config. php ') {if (is_array ($ db_config_arr_path) {$ this-> db_config = $ db_config_arr_path;} else {$ this-> db_config = require ($ db_config_arr_path );} $ this-> conn (); $ this-> table = $ This-> prefix. $ table;}/*** connect to database */private function conn () {$ db_config = $ this-> db_config; $ host = $ db_config ["DB_CONFIG"] ["DB_HOST"]; $ user = $ db_config ["DB_CONFIG"] ["DB_USER"]; $ pwd = $ db_config ["DB_CONFIG"] ["DB_PWD"]; $ db_name = $ db_config ["DB_CONFIG"] ["DB_NAME"]; $ db_encode = $ db_config ["DB_CONFIG"] ["DB_ENCODE"]; $ this-> prefix = $ db_config ["DB_CONFIG"] ["DB_PREFIX"]; $ this-> link = mysql _ Connect ($ host, $ user, $ pwd) or die ('database server connection error :'. mysql_error (); mysql_select_db ($ db_name) or die ('database connection error :'. mysql_error (); mysql_query ("set names '$ db_encode'");}/*** Data Query * parameter: the SQL function name * @ param string $ where * @ param string $ field * @ param string $ fun * @ return array * returned value: result set or result (null string returned when an error occurs) */public function select ($ where = '1', $ field = "*", $ fun = '') {$ rarr = array (); I F (empty ($ fun) {$ sqlStr = "select $ field from $ this-> table where $ where"; $ rt = mysql_query ($ sqlStr, $ this-> link); while ($ rt & $ arr = mysql_fetch_assoc ($ rt) {array_push ($ rarr, $ arr );}} else {$ sqlStr = "select $ fun ($ field) as rt from $ this-> table where $ where"; $ rt = mysql_query ($ sqlStr, $ this-> link); if ($ rt) {$ arr = mysql_fetch_assoc ($ rt); $ rarr = $ arr ['rt '];} else {$ rarr = '';} re Turn $ rarr;}/** data update * parameter: Data to be updated by the SQL condition (string or associated array) * @ param $ where * @ param $ data * @ return bool * return value: the statement is successfully executed or fails, successful execution does not affect the database */public function update ($ where, $ data) {$ ddata = ''; if (is_array ($ data )) {while (list ($ k, $ v) = each ($ data) {if (empty ($ ddata )) {$ ddata = "$ k = '$ V'";} else {$ ddata. = ", $ k = '$ V'" ;}}} else {$ ddata = $ data ;}$ sqlStr = "update $ this-> table set $ ddata whe Re $ where "; return mysql_query ($ sqlStr);}/*** add data * parameter: Data (array or associated array or string) * @ param $ data * @ return int * return value: the ID of the inserted data or 0 */public function insert ($ data) {$ field = ''; $ idata = ''; if (is_array ($ data) & array_keys ($ data )! = Range (0, count ($ data)-1) {// join array while (list ($ k, $ v) = each ($ data )) {if (empty ($ field) {$ field = "$ k"; $ idata = "'$ V'";} else {$ field. = ", $ k"; $ idata. = ", '$ V'" ;}}$ sqlStr = "insert into $ this-> table ($ field) values ($ idata )";} else {// non-correlated array or string if (is_array ($ data) {while (list ($ k, $ v) = each ($ data )) {if (empty ($ idata) {$ idata = "'$ V'";} else {$ idata. = ", '$ V'" ;}} else {// string $ idata = $ data ;} $ sqlStr = "insert into $ this-> table values ($ idata)";} if (mysql_query ($ sqlStr, $ this-> link )) {return mysql_insert_id ($ this-> link);} return 0 ;}/ *** data deletion * parameter: SQL condition * @ param $ where * @ return bool */public function delete ($ where) {$ sqlStr = "delete from $ this-> table where $ where "; return mysql_query ($ sqlStr);}/*** close MySQL connection * @ return bool */public function close () {return mysql_close ($ this-> link );}} // $ hj = new M ("user"); // echo $ hj-> insert ("NULL, 'Hj. q@qq.com ', 'cde' "); // $ arr = $ hj-> select (); // print_r ($ arr ); // echo $ hj-> update ("id> 3", array ("email" => "bn@c.cc "));
<? Phprequire 'query/M. class. php '; $ hj = new M ("user"); // Add echo $ hj-> insert (array ("NULL", "1@qq.com", "xxx ")); echo $ hj-> insert (array ("email" => "12@qq.com", "password" => "cccc"); echo $ hj-> insert ("NULL, '1970 @ qq.com ', 'cde' "); // query // view all data $ arr = $ hj-> select (); print_r ($ arr ); // view all data in the id and email fields with id greater than 3 and id less than 6 $ arr = $ hj-> select ("id> 3 and id <6", "id, email "); print_r ($ arr); // obtain the total number of records in the database $ rt = $ hj-> select (" 1 ","*", "count"); echo $ rt; // change $ B = $ hj-> update ("id = 1", "email = '2017 @ qq.com '"); echo $ B; $ B = $ hj-> update ("id> 3 and id <6", array ("email" => "1234@qq.com"); echo $ B; // delete $ B = $ hj-> delete ("id> 3"); echo $ B; // close $ hj-> close ();