PHP Classes

File: doc.txt

Recommend this page to a friend!
  Classes of Sinan Özel  >  MySQL_Control  >  doc.txt  >  Download  
File: doc.txt
Role: Documentation
Content type: text/plain
Description: Documentation
Class: MySQL_Control
Simplified MySQL Database Control
Author: By
Last change: Added explanation for the condition functions
Date: 13 years ago
Size: 3,657 bytes


Class file image Download
The class is both simple to use and contains powerful features.

Please understand the following variable format:
A Recordset, or a Set for short is an array of hashes. Each hash (associative array) has the fields in the table as keys, and the values in the record as values. This way, multiple records are saved in the memory as an array.

Here is a list of basic functions:

Connect($dbname, $login="", $password="", $hostname="localhost")
Select($table, $fields = "*", $cond = "", $end = "")
SelectRecord($table, $fields = "*", $cond = "", $end = "")
SelectSet($table, $fields = "*", $cond = "", $end = "")
Insert($table, $set)
Replace($table, $set)
Update($table, $rec, $cond, $allfields = 1)
Delete($table, $cond )
CreateTable($table, $fields, $key="", $keytype="PRIMARY KEY")
Export($table, $filename, $endfield = '', $endline = '', $enclosedby = "", $cond = "")
Import($table, $filename, $endfield = '\t', $endline = '\n', $enclosedby = "", $replace = "", $fieldlist = "", $number = "")

Here are some advanced functions:

Fetch($query): $setTables = MYSQL::Fetch("SHOW TABLES FROM test");
SelectPage($table, $fields = "*", $cond = "", $page = 0, $perpage = 10, $end = ""): Selects page by page, using the LIMIT in the SQL statement
SelectField($table, $field, $cond = "", $end = ""): Selects one field and returns an array.
MaxNumber($table, $field, $cond = ""): Selects the maximum of one field in a table.
InsertWithKey($table, $set, $pkey, $cond = ""): Inserts multiple records using a key that should be incremented with each entry. (Might cause conflicts, better to use the AUTO_INCREMENT field.)
UpdateWithKey($table, $set, $key, $allfields = 0): Updates multiple records using the primary key specified. The key should be defined in each hash, and the name of the key field should be specifed with the $key variable, either as a string or an array.
Move( $table1, $table2, $cond ): Moves from one identical table to another.
Copy( $table1, $table2, $cond ): Copies from one identical table to another.

(*) The Insert, Replace, InsertWithKey and UpdateWithKey functions all can take as input one hash (each key denoting the fieldname, and each value the value of the record) or they can take an array of hashes. (again, each key in each hash taking the value of a fieldname) Similarly, the Move and Copy function handle multiple values as well.

Condition Parsing Functions

All the condition parsings functions do the AddSlashes to avoid security holes.

parseCond($conds, $logic = "&&"): Parses an array of condition statements using the $logic as the logic operator.
eqi( $key, $value ): Case insensitive equality
eq( $key, $value ): Case sensitive equality
like( $key, $value ): LIKE operator, i.e. birth_date LIKE '1980-06%'
lt( $key, $value ): Fieldvalue Less Than Given Value
lte( $key, $value ): Fieldvalue Less Than Or Equal To Given Value
gt( $key, $value ): Fieldvalue Greater Than Given Value
gte( $key, $value ): Fieldvalue Greater Than Or Equal To Given Value
SearchCond( $arr, $parsers = "" ): This is a very useful function, especially for creating queries from values returned from HTML forms. The $arr is a hash of fields and values, but it is more complicated than that: For example, the fieldnames can have various operators, such as "<=" or ">". The hash keys can contain more than one fieldname, separated by a comma or a semi-comma, preg_split('/[,;]/'). The fieldnames can even have user defined parsing functions at the beginning. See example_2.php, it should become clear.

There are some other functions, but they are not worth explanation.