A php mysql operation class
// Database operations
- 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 ("#@__", $ 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 );
- }
- }
- ?>
|