Why use it? Examples Downloads Manual Plug-ins Other tools Support Forum Testimonials Your sites
Français 
Skrol29, 2007-03-02

TbsSQL 2.0

 

TbsSQL is a PHP class for SQL abstraction.
The goal of this tool is to reduce the code when working with SQL through PHP.
The TbsSQL is available for the following databases:
- MySQL
- MySQLi
- SQL-Server (via Ms-Odbc)
- PostgreSQL (version 7.2 or higher)


• Coding
The main asset of TbsSQL is the habitably to merge in the SQL statement a variable number of arguments and protect them against Sql Injection.

Example:
$id = 29;
$name = "boby";
$Db->Execute('UPDATE table1 SET name=@2@ WHERE (id=%1%)', $id, $name);

In this example, the SQL statement sent to the database will be: UPDATE table1 SET name='boby' WHERE (id=29)

TbsSQL supports the following jokers in your SQL statements:
    (n must be the number of the place of the argument in the TbsSQL command)
- %n% the argument will be protected against Sql Injection.
- @n@ the argument will be protected against Sql Injection and delimited as a string value with the relevant format for the database.
- #n# the argument will be converted as a date value without time with the relevant format for the database.
- ~n~ the argument will be converted as a date and time value with the relevant format for the database.

• Hook
TbsSQL is compatible with the TinyButStrong Template Engine.
Use the keyword 'tbssql' to have the MergeBlock() method using your TbsSQL instance.
Example : $TBS->MergeBlock('block1','tbssql','SELECT * FROM table1')

• License
TbsSQL is a free and open source. It is published under the LGPL license.

• Synopsis
$Db = new clsTbsSQL($srv='',$uid='',$pwd='',$db='',$drv=''); Instantiates the class. Giving connection information is optional.
$Db->Connect($srv,$uid,$pwd,$db,$drv='')
  or
$Db->Connect('glob_var');
Open a connection to the database.
Syntax 1 example:
$Db->Connect('localhost','root','xxx','db_prod');
The argument $drv is needed only for some Dabase Systems.
Syntax 2 example:
$glob_var = array('srv'=>'localhost','uid'=>'root','pwd'=>'xxx','db'=>'db_prod');
$Db->Connect('glob_var');

'glob_var' must be the name of a global variable beeing a PHP array with the keys described above.
The global variable is destroyed just after the connection.
Versioning: syntax2 is available since TbsSQL version 2.0
Note: You don't need to call this method if your connection has already been opened before. In this case, you just have to assign the connection id to property ->Id. (example: $Db->Id = $mycn). You don't have to do this assignation when PHP can work automatically with the last opened connection. This is the case for MySQL for example.
$Db->Close() Close the current connection to the database.
$Db->Execute($sql,...*...) Execute the Sql statement.
$Db->GetVal($sql,...*...) Returns the value of the first column in the first record of the query.
If no record is returned by the query, then the function returns False.
$Db->GetRow($sql,...*...) Returns an associative PHP array which is the first record returned by the query.
$Db->GetRows($sql,...*...) Returns a PHP array of all records returned by the query.
$Db->GetList($sql,...*...) Returns a PHP array with first column as keys and second column as values. If only one columns is given by the query then they the values of the array.
Example: $Db->GetList('SELECT id,name FROM t_people');
will return something like: array(1=>'Peter', 2=>'Dave', 3=>'Jane' ,...)
$Db->GetSql($sql,...*...) Returns the SQL statement with merged arguments.
$Db->LastRowId() Returns the value of the identifier generated by the last INSERT query of your connection.
$Db->AffectedRows() Returns the number of rows affected by the last UPDATE or DELETE query of your connection.
$Db->Id (read/write) The resource Id of your connection.
$Db->Mode (read/write) default value is 1.
0: silent mode, no database error message is displayed.
1: normal mode, the database error message is displayed when an error occurs.
2: debug mode, the full SQL statement is also recalled when an error occurs.
3: trace mode, all queries are displayed.
   
...*... means a variable number of arguments that will be merged in the SQL statement.

• Deprecated
$Db->Row1($sql,...*...) Same as $Db->GetRow()
$Db->Rows($sql,...*...) Same as $Db->GetRows()
$Db->Value($default,$sql,...*...) Same as $Db->GetVal() but with a default value.