PHP mysql Operation Program

Source: Internet
Author: User
Tags php mysql

A good PHP mysql operation class.

The Code is as follows: Copy code

<? Php
// Database processing class
Class db
{
// Save the data variable after SQL Execution;
Var $ db;
// Read or set the current data location
Var $ position = 0;
// Execute the SQL statement and save the result as a db variable;

Function sub_ SQL ($ str)
{
Global $ prefix; // global function, table prefix
Return str_replace ("dede _", $ 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;
}
// Read a piece of data and move the data back one bit. If the data is empty, null is returned;
Function Get_One ()
{
$ Re = empty ($ this-> db [$ this-> position])? Null: $ this-> db [$ this-> position];
$ This-> position = $ re? $ This-> position + 1: $ this-> position;
Return $ re;
}
// Determine whether the data has been read to the end
Function Judge ()
{
$ Re = empty ($ this-> db [$ this-> position])? True: false;
Return $ re;
}
// Obtain the number of databases
Function Get_Num ()
{
Return count ($ this-> db );
}
// Update the data in the database. $ t indicates the table name, $ v indicates the array format, with the field name and subscript representing the data. $ w indicates that the condition is labeled as the field name subscript as the data, $ p indicates that the condition 0 is equal, 1 is greater than, and-1 is less;
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 ))
{
Invalid w_str=htmlspecialchars(?key=.f.html specialchars ($ vaule )."'";
} Else
{
$ W_str = $ w_str. "," .htmlspecialchars(?key=.f.html specialchars ($ vaule )."'";
}
}
}
}
$ SQL = "UPDATE". $ t. "SET". $ v_str. "where". $ w_str;
Return $ result = mysql_query ($ SQL );
}
// Delete a data $ w indicates that the field name subscript is the data, $ p indicates that the condition 0 is equal, 1 is greater than, and-1 is less;
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 ))
{
Invalid w_str=htmlspecialchars(?key=.f.html specialchars ($ vaule )."'";
} Else
{
$ W_str = $ w_str. "," .htmlspecialchars(?key=.f.html specialchars ($ 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 );
}
}
?>


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.