MySQL database operations in PHP
Last Update:2018-12-07
Source: Internet
Author: User
Class dbconn {var $ conn = 0; function dbconn ($ dbhost, $ dbuser, $ dbpw, $ dbname) {$ this-> conn = mysql_connect ($ dbhost, $ dbuser, $ dbpw );! $ This-> conn & $ this-> halt ("connect to MySQL failed"); $ serverinfo = mysql_get_server_info ($ this-> conn ); if ($ serverinfo> '4. 1' & $ globals ['charset']) {mysql_query ("set character_set_connection = ". $ globals ['charset']. ", character_set_results = ". $ globals ['charset']. ", character_set_client = binary", $ this-> conn);} if ($ serverinfo> '5. 0 ') {mysql_query ("set SQL _mode ='' ", $ this-> conn);} if ($ dbname &&! @ Mysql_select_db ($ dbname, $ this-> conn) {$ this-> halt ('cannot use database');} function select_db ($ dbname) {If (! @ Mysql_select_db ($ dbname, $ this-> conn) {$ this-> halt ('cannot use database');} function server_info () {return mysql_get_server_info ($ this-> conn);} function insert_id () {$ arr = $ this-> fetch_array ('select last_insert_id () as id '); return $ arr ["ID"];} function get_value ($ SQL, $ offset = 0, $ field = 0) {$ RT = $ this-> fetch_all ($ SQL ); if (isset ($ RT [$ offset] [$ field]) {return $ RT [$ offset] [$ field];} return False;} function query ($ SQL, $ method = NULL, $ error = true) {$ query = mysql_query ($ SQL, $ this-> conn );! $ Query & $ error & $ this-> halt ('query error :'. $ SQL); return $ query;} function fetch_array ($ SQL) {$ query = $ this-> query ($ SQL); Return mysql_fetch_array ($ query );} function fetch_all ($ SQL) {$ arr = array (); $ query = $ this-> query ($ SQL); While ($ DATA = mysql_fetch_array ($ query )) {$ arr [] = $ data;} return $ arr;} function affected_rows () {return mysql_affected_rows ($ this-> conn);} function num_rows ($ SQL) {$ query = $ this-> query ($ SQL); If (! Is_bool ($ query) {return mysql_num_rows ($ query);} return 0;} function num_fields ($ SQL) {$ query = $ this-> query ($ SQL ); return mysql_num_fields ($ query);} function escape_string ($ Str) {return mysql_escape_string ($ Str);} function free_result () {$ void = func_get_args (); foreach ($ void as $ query) {If (is_resource ($ query) & get_resource_type ($ query) === 'mysql result') {mysql_free_result ($ query );}} unset ($ void);} function close () {$ this-> free_result (); Return @ mysql_close ($ this-> conn);} function halt ($ MSG = NULL) {exit ($ MSG. '<br/> '. $ SQL. '<br/> '. mysql_error ());}}