_php Instance of PHP database class

Source: Internet
Author: User
Tags php database sprintf
Copy Code code as follows:

?
Very useful PHP database class, three or four code to handle a table operation, no matter how complex the table field.
Such a large number of times used in large-scale Web site 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 results 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 (\ "Join 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 (\) does not have a 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)//Returns the number of records, resulting in a 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 (\) does not have a 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 integral 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 =\ "not input" \ "$china. \" "!\";
Exit ($reason);
}
}
function GetResult ()
{return $this->buffer;
}
function Close ()
{
Mysql_close ($this->db);
}
}
?>

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.