CopyCode 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 is frequently used on large websites. Program In terms of development, 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 );
}
}
?>