Very useful PHP database class. Copy the code as follows :? Very useful PHP database class, three or four sentences of code to deal with a table operation, no matter how complicated this table field is. This type is often used in large websites
The code is as follows:
// Very useful PHP database class, three or four statements of code to handle a table operation, no matter how complicated the table field is.
// This type of code is often used in the development of large website programs, and the effect is particularly good.
// Author: quick knife prodigal son ++
Define (\ "_ PHP_RECORD _ \", \ "exists \");
Class TRecord
{
Var $ db;
Var $ rc;
Var $ name;
Var $ value;
Var $ num;
Var $ buffer; // call the query result method $ buffer [$ I] [\ "fields \"];
Var $ seekstr; // used to save query conditions
Function TRecord ($ host = \ "localhost \", $ user = \ "root \", $ passwd = \"\")
{Global $ HTTP_POST_VARS;
$ This-> num = 0;
$ This-> host = $ host;
$ This-> user = $ user;
$ This-> passwd = $ passwd;
If ($ this-> db = mysql_connect ($ host, $ user, $ passwd) = false)
Exit (\ "error connecting database! \");
While (list ($ this-> name [$ this-> num], $ this-> value [$ this-> num]) = each ($ HTTP_POST_VARS ))
{$ This-> num ++;
}
//////////////
For ($ I = 0; $ I <$ this-> num; $ I ++)
{$ This-> value [$ I] = $ this-> SafeString ($ this-> value [$ I]);
}
//
}
Function SafeString ($ message)
{$ Message = str_replace (\ "\", \ "\", $ message );
$ Message = str_replace (\ "<\", \ "<\", $ message );
$ Message = str_replace (\ ">\", \" >\", $ message );
// $ Message = str_replace (\ "| \", \ "| \", $ message );
// $ Message = str_replace (\ ", \" "\", $ message );
// $ Message = nl2br ($ message );
Return $ message;
}
//////
Function reset ()
{$ This-> num = 0;
$ This-> name = array ();
$ This-> value = array ();
}
Function add ($ name, $ values)
{$ This-> name [$ this-> num] = $ name;
$ This-> value [$ this-> num] = $ values;
$ This-> num ++;
}
Function unadd ($ name)
{$ J = 0;
For ($ I = 0; $ I <$ this-> num; $ I ++)
{If ($ this-> name [$ I]! = $ Name)
{$ Aaa [$ j] = $ this-> name [$ I];
$ Bbb [$ j] = $ this-> value [$ I];
$ J ++;
}
}
$ This-> name = $ aaa;
$ This-> value = $ bbb;
$ This-> num = $ j;
}
Function InsertRecord ($ database, $ table)
{Mysql_select_db ($ database );
If ($ this-> num = 0)
Exit (\ "no variable defined! \");
$ Field = implode (\ ", \", $ this-> name );
For ($ I = 0; $ I <$ this-> num; $ I ++)
{If (is_string ($ this-> value [$ I])
$ Ls [$ I] = \ "\ '\". $ this-> value [$ I]. \ "\' \";
Else
$ Ls [$ I] = $ this-> value [$ I];
$ Value = implode (\ ", \", $ ls );
}
$ SQL = sprintf (\ "insert into % s (% s) values (% s) \", $ table, $ field, $ value );
If (mysql_query ($ SQL, $ this-> db) = false)
{Echo \ "an error occurred while writing data to the database: \". $ SQL;
Exit ();
}
}
Function SelectRecord ($ database, $ table) // number of returned Records. The results are in the buffer zone.
{Mysql_select_db ($ database );
If ($ this-> num = 0)
$ SQL = sprintf (\ "select * from % s \", $ table );
Else
{
For ($ I = 0; $ I <$ this-> num; $ I ++)
{If (is_string ($ this-> value [$ I])
$ Ls [$ I] = \ "\ '\". $ this-> value [$ I]. \ "\' \";
Else
$ Ls [$ I] = $ this-> value [$ I];
$ Str [$ I] = sprintf (\ "% s = % s \", $ this-> name [$ I], $ ls [$ I]);
}
$ String = implode (\ "and \", $ str );
$ This-> seekstr = $ string;
$ SQL = sprintf (\ "select * from % s where % s \", $ table, $ string );
}
If ($ rc = mysql_query ($ SQL, $ this-> db) = false)
{Echo \ "an error occurred while querying the database: \". $ SQL;
Exit ();
}
$ I = 0;
While ($ this-> buffer [$ I] = mysql_fetch_array ($ rc ))
{
$ I ++;
}
Mysql_free_result ($ rc );
Return $ I;
}
Function UpdateRecord ($ database, $ table, $ limitstr)
{Mysql_select_db ($ database );
If ($ this-> num = 0)
Exit (\ "no variable defined! \");
For ($ I = 0; $ I <$ this-> num; $ I ++)
{If (is_string ($ this-> value [$ I])
$ Ls [$ I] = \ "\ '\". $ this-> value [$ I]. \ "\' \";
Else
$ Ls [$ I] = $ this-> value [$ I];
$ Upstr [$ I] = $ this-> name [$ I]. \ "= \". $ ls [$ I];
}
$ Str = implode (\ ", \", $ upstr );
$ SQL = sprintf (\ "update % s set % s where % s \", $ table, $ str, $ limitstr );
If (mysql_query ($ SQL, $ this-> db) = false)
{Echo \ "an error occurred while modifying data: \". $ SQL;
Exit ();
}
}
Function addtip ($ database, $ table, $ fileds, $ limitstr = \"\")
{// It must be an integer field
Mysql_select_db ($ database );
If ($ limitstr! = \"\")
$ SQL = sprintf (\ "update % s set % s = % s + 1 where % s \", $ table, $ fileds, $ fileds, $ limitstr );
Else
$ SQL = sprintf (\ "update % s set % s = % s + 1 \", $ table, $ fileds, $ fileds );
If (mysql_query ($ SQL, $ this-> db) = false)
{Echo \ "an error occurred while modifying data: \". $ SQL;
Exit ();
}
}
Function unaddtip ($ database, $ table, $ fileds, $ limitstr = \"\")
{
Mysql_select_db ($ database );
If ($ limitstr! = \"\")
$ SQL = sprintf (\ "update % s set % s = % S-1 where % s \", $ table, $ fileds, $ fileds, $ limitstr );
Else
$ SQL = sprintf (\ "update % s set % s = % S-1 \", $ table, $ fileds, $ fileds );
If (mysql_query ($ SQL, $ this-> db) = false)
{Echo \ "an error occurred while modifying data: \". $ SQL;
Exit ();
}
}
Function isempty ($ var, $ china)
{If (trim ($ var) == \"\")
{
$ Reason = \ "no input" \ ". $ china .\""! \";
Exit ($ reason );
}
}
Function GetResult ()
{Return $ this-> buffer;
}
Function close ()
{
Mysql_close ($ this-> db );
}
}
?>
The http://www.bkjia.com/PHPjc/320085.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/320085.htmlTechArticle code is as follows :? // Very useful PHP database class, three or four statements of code to handle a table operation, no matter how complicated the table field is. // This type is frequently used on large websites...