Using Database

This page covers running SQL queries and creating or dropping tables through epesi's DB class.

Run any SQL query with the static DB::Execute() method:

DB::Execute($query);
// example:
DB::Execute('SELECT * FROM mytable WHERE myfield=5');

When passing arguments to Execute(), use printf-style placeholders to keep queries safe:

DB::Execute($query,$array_of_values);
// example:
DB::Execute('SELECT * FROM mytable WHERE field1=%s AND field2=%d',array($field1,$field2));

DB also provides functions that simplify common tasks. The most useful ones:

DB::CreateTable('<table_name>','<fields>',array('constraints'=>'<constraints>'));
// example:
DB::CreateTable('my_module__my_table',
    'id I4 AUTO KEY,'.
    'name C(64) NOTNULL,'.
    'user I4 NOT NULL',
    array('constraints'=>', FOREIGN KEY (user) REFERENCES user_login(id)'));

Table creation usually belongs in <module name>Install.php, inside install(). Add the matching table deletion in the same file's uninstall() function, using:

DB::DropTable('<table_name>');