Fabriq Framework Docs

This page displays the APIs for database interactions that are made available by the /core/DatabasepgSQL.class.php file's Fabriq class. Each function available for use is listed below with examples of how to use the function.

The default database connection is created automatically by the Fabriq framework. Connections to other databases can be made by instantiating a new instance of the Database class.

As of version 1.1, MySQL is the default database type for use with Fabriq. For information on using MySQL, see the DatabaseMySQL.class.php page.

close()

Description

This function closes the database connection. It is automatically called at the end of execution for the Fabriq framework for the default connection.

Example(s)

// close database connection
$db_var_name->close();

Added in version: 1.1

escape_string($str)

Parameters

  • str – string to be escaped before added to a query

Description

Escapes a string so that it is safe to use in a SQL query. This function is useful for helping to prevent SQL injections and malicious database queries from being executed.

Example(s)

$str = $db_var_name->escape_string("escape this string so that '; DELETE * FROM table' can't be executed");

Added in version: 1.1

Return type: string

prepare_cud($sql, $inputs)

Parameters

  • sql – the SQL query to be prepared and executed
  • inputs – array of inputs to be prepared and inserted into the query

Description

Prepares SQL queries to be executed. This function is used for preparing insert, update, and delete queries. The inputs are all escaped and then inserted into the input query before the query is executed.

Example(s)

// example prepared input query
$sql = 'INSERT INTO table ("col1", "col2", "col3") VALUES ($1, $2, $3)';
$inputs = array("variable '1'", 'var2', "variable '3'");
$db_var_name->prepare_cud($sql, $inputs);

Added in version: 1.1

Return type: boolean

prepare_select($sql, $fields, $inputs = array(), $attributes = NULL)

Parameters

  • sql – the select SQL query to be executed
  • fields – the database columns to be selected
  • inputs (optional) default: array() (empty array) – the input parameters to be bound to the select statement
  • attributes (optional) default: NULL – if you want the results to be returned as an object instead of an array, pass in an array of attributes for the collection to map each object's variables to

Description

The prepare_select() function is used to prepare select statements. By default, this function returns the results in an associative array. If an array of attributes is passed in, the function will return the result as a collection of objects.

Example

// simple example
$sql = 'SELECT * FROM table';
$fields = array('id', 'col1', 'col2', 'col3');
$results = $db_var_name->prepare_select($sql, $fields);
 
// example with inputs
$sql = 'SELECT "firstname", "lastname" FROM table WHERE "id"=$1';
$fields = array('firstname', 'lastname');
$inputs = array(1234);
$result = $db_var_name->prepare_select($sql, $fields, $inputs);
 
// get the results as an object
// this example would be called from inside of a model class' function
$sql = 'SELECT * FROM table WHERE "id"=$1';
$inputs = array(1234);
$this->data = $db_var_name->prepare_select($sql, $this->fields(), $inputs, $this->fields());

Added in version: 1.1

query($sql)

Parameters

  • sql – the SQL query to be executed

Description

Executes the given query. It is recommended to use the prepared functions when possible. When a query is executed, its result is stored in the database variable's result attribute.

Example(s)

$sql = "SELECT * FROM table"
$db_var_name->query($sql);
// to use the result, call $db_var_name->result;

Added in version: 1.1

__construct($db_info)

Parameters

  • $db_info – database connection settings that are stored in the config file

Description

Database constructor. This function is automatically called by the Fabriq framework for the default database connection. If the developer wants to connect to more than one database, a new instance can be created. Be sure that when using multiple databases, that the connections are closed.

Example(s)

// example database connection
$db_var_name = new Database($_FDB['db_settings_name']);
// execute other code
// close the connection
$db_var_name->close();

Added in version: 1.1