Home  >  Article  >  Backend Development  >  Encapsulation of PHP connection to database

Encapsulation of PHP connection to database

巴扎黑
巴扎黑Original
2016-11-23 10:42:381569browse


/**

* @Author:jilongliang

* @Date:2012/09/17

* @Description: Database encapsulation

* @Version:version1.0

*/

/*

* Import configuration file

*/

require '../config/config.php';

// require 'org.php/util/global.php';

/**

* Get database connection

*/

function Connect(){

//global means global variable, the intention is to use this variable in the function It can also be accessed externally

global $_conn;

if (!$_conn = @mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD)) {

exit('Database connection failed');

}

}

/* *

* select_db selects a database

* @return void

*/

function Select_Db() {

if (!mysql_select_db(DB_DATABASE)) {

exit('The specified database cannot be found');

}

}

/**

* Set the field encoding of the database

*/

function Set_Names() {

if (!mysql_query('SET NAMES UTF8')) {

exit('Character set error');

}

}

/**

*Query

*/

function Query($_sql) {

if (!$_result = mysql_query($_sql)) {

exit('SQL execution failed'.mysql_error());

}

return $_result;

}

/**

* fetch_array can only obtain one data group from the specified data set

* @param $_sql

*/

function Fetch_array($_sql) {

Return mysql_fetch_array(Query($_sql),MYSQL_ASSOC);

}

/* *

* fetch_array_list can return all data in the specified data set

* @param $_result

*/

function Fetch_array_list($_result) {

return mysql_fetch_array($_result,MYSQL_ASSOC);

}

/**

* Number row

*/

function Num_rows($_result) {

Return mysql_num_rows($_result);

}

/**

* affected_rows indicates the number of records affected

*/

function Affected_rows() {

Return mysql_affected_rows();

}

/**

* free_result destroys (releases) the result set

* @param $_result

*/

function Free_result($_result) {

mysql_free_result($_result);

}

/**

 * insert_id

 */

function Insert_id() {

return mysql_insert_id();

}

/**

*Array SQL to determine whether it is duplicated

* @param $_sql

* @param $_info

*/

function Is_repeat($_sql,$_info) {

if (Fetch_array($_sql)) {

alert_back($_info);

}

}

/**

* Release resources

*/

function ResaseResource() {

if (!mysql_close()) {

exit('Close exception');

}

}

/**

* alert_back() table is a JS pop-up window

* @access public

* @param $_info

* @return void pop-up window

*/

function alert_back($_info) {

echo "