PHP database operations
$ Db = new mysql ($ db_host, $ db_user, $ db_password, $ db_table, $ db_conn, $ pre, $ coding );
Class mysql {
Private $ db_host;
Private $ db_user;
Private $ db_password;
Private $ db_table;
Private $ db_conn; // database connection ID;
Private $ result; // result resource ID for executing the query command
Private $ SQL; // SQL Execution statement
Private $ pre; // database table prefix
Private $ coding; // database encoding, GBK, UTF8, gb2312
Function _ construct ($ db_host, $ db_user, $ db_password, $ db_table, $ db_conn, $ pre, $ coding ){
$ This-> db_host = $ db_host;
$ This-> db_user = $ db_user;
$ This-> db_password = $ db_password;
$ This-> db_table = $ db_table;
$ This-> db_conn = $ db_conn;
$ This-> pre = $ pre;
$ This-> coding = $ coding;
$ This-> connect ();
}
Function connect (){
$ This-> db_conn = @ mysql_connect ($ this-> db_host, $ this-> db_user, $ this-> db_password) or die ($ this-> show_error ("Database Link error, please check the database link configuration! "));
If (! Mysql_select_db ($ this-> db_table, $ this-> db_conn )){
Echo "no data table found:". $ this-> db_table;
}
Mysql_select_db ($ this-> db_table, $ this-> db_conn );
$ This-> query ("set names $ this-> coding ");
}
/* Functions used to execute SQL statements */
Function query ($ SQL ){
If (emptyempty ($ SQL )){
$ This-> show_error ("Your SQL statement cannot be blank! ");
} Else {
$ This-> SQL = $ SQL;
}
$ Result = mysql_query ($ this-> SQL, $ this-> db_conn );
Return $ this-> result = $ result;
}
/* Create and add a new database */
Public function create_database ($ database_name ){
$ Database = $ database_name;
$ SqlDatabase = 'create database'. $ database;
Return $ this-> query ($ sqlDatabase );
}
// Calculate the number of result sets based on the select query results
Public function db_num_rows (){
If ($ this-> result = null ){
If ($ this-> show_error ){
$ This-> show_error ("SQL statement error! ");
}
} Else {
Return mysql_num_rows ($ this-> result );
}
}
/* Query all databases on the server */
// Separate the system database from the user database for more intuitive display?
Public function show_databases (){
$ This-> query ("show databases ");
Echo "existing database:". $ amount = $ this-> db_num_rows ($ rs );
Echo "";
$ I = 1;
While ($ row = $ this-> fetch_array ($ rs )){
Echo "$ I $ row [Database]";
Echo "";
$ I ++;
}
}
// Returns the names of all databases on the host in an array.
Public function databases ()
{
$ RsPtr = mysql_list_dbs ($ this-> db_conn );
$ I = 0;
$ Cnt = mysql_num_rows ($ rsPtr );
While ($ I <$ cnt)
{
$ Rs [] = mysql_db_name ($ rsPtr, $ I );
$ I ++;
}
Return print_r ($ rs );
}
/* Query all tables in the database */
Function show_tables ($ database_name ){
$ This-> query ("show tables ");
Echo "existing database:". $ amount = $ this-> db_num_rows ($ rs );
Echo "";
$ I = 1;
While ($ row = $ this-> fetch_array ($ rs )){
$ ColumnName = "Tables_in _". $ database_name;
Echo "$ I $ row [$ columnName]";
Echo "";
$ I ++;
}
}
/*
Mysql_fetch_row () array $ row [0], $ row [1], $ row [2]
Mysql_fetch_array () array $ row [0] or $ row [id]
Mysql_fetch_assoc () array is case sensitive with the $ row-> content field
Mysql_fetch_object () object is case sensitive with $ row [id] and $ row [content] fields
*/
/* Obtain the record set and array-index and association. use $ row ['content'] */
Public function fetch_array ()
{
Return @ mysql_fetch_array ($ this-> result );
}
// Obtain the joined array, using $ row ['field name']
Public function fetch_ass ()
{
Return @ mysql_fetch_assoc ($ this-> result );
}
// Obtain the numeric index array, using $ row [0], $ row [1], $ row [2]
Public function fetch_row ()
{
Return @ mysql_fetch_row ($ this-> result );
}
// Obtain the object Array Using $ row-> content
Public function fetch_Object ()
{
Return @ mysql_fetch_object ($ this-> result );
}
// Simplified query select
Public function findall ($ table ){
$ Table = $ this-> fulltablename ($ table );
$ This-> query ("select * from $ table ");
}
Public function select ($ table, $ columnName, $ condition ){
$ Table = $ this-> fulltablename ($ table );
If (emptyempty ($ columnName )){
$ ColumnName = "*";
}
$ This-> query ("SELECT $ columnName FROM $ table $ condition ");
}
// Simplified insert
Function insert ($ table, $ arr ){
$ Table = $ this-> fulltablename ($ table );
$ SQL = "INSERT INTO $ table ";
If (! Is_array ($ arr )){
$ This-> show_error ("Enter the parameter array! ");
} Else {
$ K = "";
$ V = "";
Foreach ($ arr as $ key => $ value ){
$ K. = "'$ key ',";
$ V. = "'". $ value ."',";
}
}
$ SQL = $ SQL. "(". substr ($ k, 0,-1 ). ") VALUES (". substr ($ v, 0,-1 ). ")";
$ This-> query ($ SQL );
}
// Simplified update
Function update ($ table, $ arr, $ where ){
$ Table = $ this-> fulltablename ($ table );
$ SQL = "UPDATE $ table SET ";
If (! Is_array ($ arr )){
$ This-> show_error ("Enter the parameter array! ");
} Else {
Foreach ($ arr as $ key => $ value ){
$ SQL. = "'". $ key. "' = '". $ value ."',";
}
}
$ SQL = substr ($ SQL, 0,-1). "where". $ where;
Return $ this-> query ($ SQL );
}
// Simplified delete
Function delete ($ table, $ where = ""){
$ Table = $ this-> fulltablename ($ table );
If (emptyempty ($ where )){
$ This-> show_error ("the condition cannot be blank! ");
} Else {
$ Where = "where". $ where;
}
$ SQL = "DELETE FROM $ table". $ where;
// Echo $ SQL;
Return $ this-> query ($ SQL );
}
// Obtain the ID generated by the previous INSERT operation
Public function insert_id (){
Return mysql_insert_id ();
}
// Data table with a prefix
Public function fulltablename ($ table ){
Return $ table = $ this-> pre. $ table;
}
// Query the number of fields
Public function num_fields ($ table ){
$ Table = $ this-> fulltablename ($ table );
$ This-> query ("select * from $ table ");
Echo "";
Echo "number of fields:". $ total = mysql_num_fields ($ this-> result );
Echo"
";
For ($ I = 0; $ I <$ total; $ I ++ ){
Print_r (mysql_fetch_field ($ this-> result, $ I ));
}
Echo "";
Echo "";
}
// Obtain MySQL Server Information
Public function mysql_server ($ num = ''){
Switch ($ num ){
Case 1:
Return mysql_get_server_info (); // MySQL Server Information
Break;
Case 2:
Return mysql_get_host_info (); // obtain MySQL host information
Break;
Case 3:
Return mysql_get_client_info (); // obtain MySQL client information
Break;
Case 4:
Return mysql_get_proto_info (); // obtain MySQL protocol information
Break;
Default:
Return mysql_get_client_info (); // obtain the mysql version by default.
}
}
// Destructor, which automatically closes the database and recycles garbage
/* Public function _ destruct ()
{
If (! Empty ($ this-> result )){
$ This-> free ();
}
Mysql_close ($ this-> $ db_conn );
}*/
/* Obtain the real IP address of the client */
Function getip (){
If (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown "))
{
$ Ip = getenv ("HTTP_CLIENT_IP ");
}
Else if (getenv ("HTTP_X_FORWARDED_FOR") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown ")){
$ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
}
Else if (getenv ("REMOTE_ADDR") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown "))
{
$ Ip = getenv ("REMOTE_ADDR ");
}
Else if (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], "unknown ")){
$ Ip = $ _ SERVER ['remote _ ADDR '];
}
Else {
$ Ip = "unknown ";
}
Return ($ ip );
}
Function show_error ($ str ){
Echo "Javascript"> alert ('". $ str."'); history. back (-1 );";
}
}
?>