? 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 of code has been widely used in the development of large website programs for many times, and the effect is particularly good. Author: Clipper prodigal son + + define (_ PHP_RECORD _, exists); classTRecord {var $ db; var $ rc; var $ name; var $ value; var // 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 );
}
}
?>
From: China black guest Alliance-Yunnan sub-station
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service