MYSQL Utils v1.0

 

This class can:

  • access mysql databases
  • outputs customisable tables from the mysql query
  • save or outputs data from the mysql query in xml format with records appended as attributes or xml childs, very usefull for actionscript programmers

Author: Ciprian Voicu
E-Mail: voicuciprian at yahoo dot com
Cluj-Napoca, Romania
http://www.autoportret.ro
April 2006

This script use the BSD license. Redistribute it only if this copyright statement is not removed

 

I. MYSQL

 

function Query(string query) ;

This function has one parameter that is the mysql query. Because the mysql language is very complex I think that for this simple class is enought to keep this simple mode of access.

EX: $db->Query('SELECT * FROM `table`');

 

function FetchMode(int mode) ;

This function change the fetch mode of the resulting sql query from the Query function

0 = MYSQL_NUM
1 = MYSQL_ASSOC
2 = MYSQL_BOTH
by default is set to 1

EX: $db->FecthMode(1);

 

function Fetch() ;

This function fetches the sql query result from the Query() function.

EX: while($row=$db->Fetch()){
            //do something
       }

 

function Insert(string query, array values) ;

This function get the query string, replace the '??' with the values from the second parameter after these values was modified with mysql_real_escape_string. After that the new resulted string is sent the the Query function. It can be used either the Query function but this one is more safe against the SQL Injection attack.

EX: $db->Insert('INSERT INTO `table` (`id`, 'username', `description`) VALUES ('', '??', '??');', array('ciprian', 'this " is my descrption'));

 

function Count(string query) ;

This function sends the query to Query string and retrieve the number of records.

EX: $nr=$db->Count('SELECT COUNT(*) FROM `table` WHERE `id`=5');

 

function NumRows() ;

Retrive the number of rows from a SELECT query.

EX: $nr=$db->NumRows();

 

function NumFields() ;

Retrive the number of fields from a SELECT query.

EX: $nr=$db->NumFields();

 

function LastId() ;

Retrive the last id inserted into the database.

EX: $nr=$db->LastId();

 

function Affected() ;

Retrive the number of affected rows from the INSERT, DELETE and UPDATE query.

EX: $nr=$db->Affected();

 

function Close() ;

Close the database .

EX: $db->Close();

 

 

II. TABLE

 

function Table(string query) ;

This function generates a table from the string query iserted as parameter with the field names as table header names.

EX: $db->Table("SELECT * FROM `table`");

 

function AddTbStyle(string stylename, string stylevalue) ;

This function add a style to the table element.

EX: $db->AddTbStyle('border', '1px solid #FFCC00');

 

function AddThStyle(string stylename, string stylevalue) ;

This function add a style to the th element.

EX: $db->AddThStyle('background, '#FFCC00');

 

function AddTdStyle(string stylename, string stylevalue [, string field]) ;

This function add a style to the td element. If the field parameter is present the style will be applied only to that field, else the style will be applied to all fields.

EX: $db->AddTdStyle('color, '#FFCC00');

 

function AddTrBg(string color) ;

This function add a color to the tr element. If the function is called more times with different values the colors will be alternated.

EX: $db->AddTrBg('#FFCC00');

 

function AddClass(string element, string, classname [, string field] ) ;

This function add a class to a specific element. Possible elements values are 'table', 'tr', 'td', 'th'. For the 'td' elements it can be specified the field name and the class will be applied only for that field. If this function will pe called more times for the tr element the classes wil alternate. NOTE that if the class is added to element all the styles added with the specific add style function will be ignored.

EX: $db->AddClass('table', 'class1');

                       

function ThLables(strings names) ;

With this function it can be changed the table header names from the output table. If you want to change just for the second specify an empty string for the first one, and so on.

EX: $db->AddLabels('','my name','',another');

                       

 

III. XML

 

function XML(string query [, int mode]) ;

This class is usefull for those who need the mysql data into an XML format. Either for the actionscript programmers who need to interact with the database. If is called from flash with the XML.sendAndLoad() function the class will send back to the flash an object in xml format. The second argument change the mode in which the data will be appended to the xml file. If NULL or 1 the data will be appended as attributes, else if mode is 2 the data wil be appended as child nodes. The default values is 1.

EX: $db->XML('SELECT * FROM `table`', 2);

 

function XMLSave(string path) ;

With this function the xml string can be saved as xml file on the server.                       

EX: $db->XMLSave('folder/folder/data.xml');

 

function XMLGet([int mode]) ;

If mode argument is present or is 1 the data will be outputed in browser. If mode argument is 2 the browser will promt for download. The name of the file will be name of the table from the query string.

EX: $db->XMLGet(2);