Very good PHP database class _php Tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
Very good PHP database class, three or four code to take care of a table operation, no matter how complex the field.
This kind of many times used in large-scale website program development, the effect is particularly good.
Author: fast-Knife Prodigal +
Define (\ "_php_record_\", \ "Exists\");
Class Trecord
{
var $db;
var $RC;
var $name;
var $value;
var $num;
var $buffer; Query result call method $buffer [$i][\ "fields\"];
var $seekstr; Save query conditions with
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 (\ "Junction database error!\");
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 \ "Error writing data to database: \". $sql;
Exit ();
}
}
function Selectrecord ($database, $table)//return record count, result in buffer
{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 \ "error querying 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 \ "error modifying data: \". $sql;
Exit ();
}
}
function Addtip ($database, $table, $fileds, $limitstr =\ "\")
{//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 \ "error 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 \ "error modifying data: \". $sql;
Exit ();
}
}
Function IsEmpty ($var, $china)
{if (Trim ($var) ==\ "\")
{
$reason =\ "no entry" \ "$china. \" "!\";
Exit ($reason);
}
}
function GetResult ()
{return $this->buffer;
}
function Close ()
{
Mysql_close ($this->db);
}
}
?>

http://www.bkjia.com/PHPjc/320085.html www.bkjia.com true http://www.bkjia.com/PHPjc/320085.html techarticle Copy the code as follows:?//Very good PHP database class, three or four code to take care of a table operation, no matter how complex the field. This class is used many times in large Web sites ...

  • Contact Us

    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

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.