UTILS CLASS FOR THE PHP LIB
LEGAL STUFF:
* utilsClass.inc version 0.1 copyright(C) 2002 Fiorenza Caetani
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*
* Fiorenza Caetani [webmaster at larosmarina.it]
*
* Utils class for phpLib, provide 4 methods to select, insert, update and delete
* data from database. It requires phpLib properly installed. Refer to the
* documentation with this file for further instructions (but it's so simple!).
*
* For more information about phpLib and how to use it,
* you can look at http://sourceforge.net/projects/phplib/
This package contains these files:
utilsClass.inc
GNU_license
readme.txt
HOW TO USE IT
Save the file utilsClass.inc in the php directory of your phpLib (where you put all the classes files). Require it in your prepend.php3 file, at any point after the require of your local.inc file.
To instantiate the class Utils, you need to pass it one required parameter, the name of the extension you have made of the db_sql class in your local.inc file. Here's how to do it:
$my_utils = new Utils($db_class_name);
Each method has some required parameters.
The do_select() method returns an array with the values extracted from the database and cleaned from slashes with strip_slashes(). To access these values I usually use a for cicle if I know that I'll have more than one record returned. Here's an example of usage of the do_select() method:
<?php
$my_utils = new Utils('my_db');
$fields = array(
0 => 'field_one',
1 => 'field_two',
2 => 'field_three'
);
$tables = array(
0 => 'table_one',
1 => 'table_two'
);
$cond = 'where table_one.field_one = table_two.field_two and table_one.field_one = '.$some_value;
$res = $my_utils->do_select($tables,$fields,$cond);
$num_res = count($res);
for($i=0;$i<$num_res;$i+=3)
{
$field_one = $res[$i];
$field_two = $res[$i+1];
$field_three = $res[$i+2];
echo $field_one.' - '.$field_two.' - '.$field_three.'<hr>';
}
?>
In the same way, I use the do_update(), do_insert() and do_delete() methods. ATTENTION: when you pass a string value in the array of values to the do_insert() or do_update method, you need to properly pass the apixes too. Usually I do this in this way:
<?php
$my_utils = new Utils('my_db');
$fields = array(
0 => 'field_one',
1 => 'field_two',
2 => 'field_three'
);
$values = array(
0 => '"'.$value_one.'"', // a string
1 => $value_two, // a number
2 => '"'.$value_three.'"' // another string
);
$cond = 'some condition here';
$res = $my_utils->do_insert('table_name',$fields,$values);
?>
ATTENTION: currently, only the do_select() method support the usage of more than one table each time, this is why you need to pass the table name as an array to this method. The other methods can use only one table each time (and you pass them table name as a string).
PLATFORM:
I've developed and tested this class on Windows 2000, with Apache 1.3, PHP 4.1, MySQL 3.23 and phpLib 7.2. I've used it on several Apache servers with Unix OS (mostly Linux Red Hat) and MySQL, but I can't say it's widely tested. But it's a so simple thing, so I don't see why it shouldn't works on other types of platform and databases, as long as you can have a phpLib working on it.
FURTHER DETAILS:
You can find more details about each method in the source file utilsClass.inc
TO DO:
- support for multiple tables usage in do_update(), do_insert() and do_delete() method.
- automatic detect of type of variable $tb_name, so you can pass it as a simple string if you need to work just on one table.
- support for multiple types of cleaning and formatting output and input
CREDITS
All credits for this little work go to the staff of phpLib, I've developed utilsClass just to work faster with this fantastic software library. Any comment or suggestion is welcome at this address: webmaster at larosmarina.it |