PHP MySQL database operation class

Source: Internet
Author: User
Tags php mysql mysql database
The code is as follows Copy Code

<?php
Database processing Classes
Class db
{
The data save variables after SQL execution;
var $db;
Reads or sets the location of the current data
var $position = 0;
Execute the SQL statement and save the result as a DB variable;

function Sub_sql ($STR)
{
Global $prefix;//globally function, table prefix
Return Str_replace ("#@__", $prefix, $STR);
}
function Sql ($STR)
{
$str = $this->sub_sql ($STR);
$result = mysql_query ($STR);
$i = 0;
while ($row = Mysql_fetch_array ($result))
{
$str _array[$i]= $row;
$i + +;
}
if (Empty ($str _array))
{
$str _array=array ();
}
$this->db= $str _array;
}
Reads a piece of data and moves the data backward one bit, and returns null if the data is empty;
function Get_one ()
{
$re =empty ($this->db[$this->position])? null: $this->db[$this->position];
$this->position= $re? $this->position+1: $this->position;
return $re;
}
To determine if the data is read to the end.
function Judge ()
{
$re =empty ($this->db[$this->position])? True:false;
return $re;
}
Get the number of db inside
function Get_num ()
{
Return count ($this->db);
}
Update the data inside the database, $t for the table name, $v format for the array format, superscript as the field name, subscript as the data, $w as the criteria for the field name marked as data, $p for the condition 0 equals sign, 1 is greater than,-1 is less than;
function Set_updata ($t, $v, $w, $p =0)
{
$this->sql ($t);
$v _str= "";
$w _str= "";
$f = "";
foreach ($v as $key => $vaule)
{
if (!is_numeric ($key))
{
if (Empty ($v _str))
{
$v _str=htmlspecialchars ($key). " = ' ". Htmlspecialchars ($vaule)." ' ";
}else
{
$v _str= $v _str. ",". Htmlspecialchars ($key). " = ' ". Htmlspecialchars ($vaule)." ' ";
}
}
}
Switch ($p)
{
Case 0:
$f = "=";
Break
Case 1:
$f = ">";
Break
Case-1:
$f = "<";
Break
}
if (!empty ($f))
{
foreach ($w as $key => $vaule)
{
if (!is_numeric ($key))
{
if (Empty ($v _str))
{
$w _str=htmlspecialchars ($key). $f. Htmlspecialchars ($vaule). "'";
}else
{
$w _str= $w _str. ",". Htmlspecialchars ($key). $f. Htmlspecialchars ($vaule). "'";
}
}
}
}
$sql = "UPDATE". $t. " SET ". $v _str." where ". $w _str;
return $result = mysql_query ($sql);
}
Delete a data $w is labeled as a field name as data, $p is the equal sign for condition 0, 1 is greater than,-1 is less than;
function Set_del ($t, $w, $p =0)
{
$this->sub_sql ($t);
$w _str= "";
$f = "";
Switch ($p)
{
Case 0:
$f = "=";
Break
Case 1:
$f = ">";
Break
Case-1:
$f = "<";
Break
}
if (!empty ($f))
{
foreach ($w as $key => $vaule)
{
if (!is_numeric ($key))
{
if (Empty ($v _str))
{
$w _str=htmlspecialchars ($key). $f. Htmlspecialchars ($vaule). "'";
}else
{
$w _str= $w _str. ",". Htmlspecialchars ($key). $f. Htmlspecialchars ($vaule). "'";
}
}
}
}
$str = "DELETE from". $t. " WHERE ". $w _str;
return $result = mysql_query ($STR);
}
function Add ($t, $v)
{
$this->sub_sql ($t);
$k _str= "";
$v _str= "";
foreach ($v as $key => $vaule)
{
if (!is_numeric ($key)) {
if (Empty ($k _str))
{
$k _str=htmlspecialchars ($key);
$v _str= "'". Htmlspecialchars ($vaule). "'";
}else
{
$k _str= $k _str. ",". Htmlspecialchars ($key);
$v _str= $v _str. ",". "'". Htmlspecialchars ($vaule). "'";
}
}
}
$str = "INSERT into". $t. " (". $k _str.") "." Value (". $v _str.");
return $result = mysql_query ($STR);
}
}
?>

Related Article

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.