Cyclone3 Skin

Make a database request

From Cyclone3 Wiki

Cyclone3 uses a standardized database requesting functionality. If you want to request a database, you can do so using:

my %db_handler=TOM::Database::SQL::execute(
   qq{
      SELECT
         ID
      FROM
         `TOM`.`a010_mytable`
      WHERE
             ID=?
         AND name LIKE ?
      LIMIT
         1
   },
   'bind'=>[
      $ID,
      $name
   ],
   'quiet'=>1
);

which will prepare and execute the database request for you. You have probably noticed, that there are no values in the SQL and that there are some questionmarks instead. This is for security reasons, avoiding malicious SQL injections and stuff. For each questionmark, there is an item in the bind array, which replaces the questionmarks in the same order. This way, you really don't have to escape things to be safe.

Then there's the quiet variable, which makes the request non-verbose - it won't get logged. The default value is 0 - verbose.

After you make a request, you can process the returned result:

if (my %db_line=$db_handler{'sth'}->fetchhash())
{
   foreach(keys $db_line)
   {
      # ...
   }
}

there's a few other variables you can access on the returned database handler, namely:

  • $db_handler{'sth'}->{'err'}

error returned by database

  • $db_handler{'sth'}->{'rows'}

response rows count returned by database

  • $db_handler{'sth'}->{'sql'}

response rows count returned by database

  • $db_handler{'sth'}->{'db_h'}

database host