Fabriq Framework Docs

This page displays the APIs for database interactions that are made available by the /core/DatabaseMySQL.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 PostgreSQL instead, see the DatabasepgSQL.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();

errno()

Description

This function returns the last error number returned by the database or boolean FALSE if there is no error.

Example(s)

// get database error number
$db_var_name->errno();

Added in version: 0.9

Return type: integer

error()

Description

This function returns the last error returned by the database or boolean FALSE if there is no error.

Example(s)

// get database error
$db_var_name->error();

Added in version: 0.9

Return type: string

error_str()

Description

This function returns the last error number and error returned by the database or boolean FALSE if there is no error.

Example(s)

// get database error
$db_var_name->error_str();

Added in version: 0.9

Return type: string

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");

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 (?, ?, ?);";
$inputs = array("variable '1'", 'var2', "variable '3'");
$db_var_name->prepare_cud($sql, $inputs);

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`=?";
$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`=?";
$fields = array('id', 'firstname', 'lastname', 'zipcode');
$inputs = array(1234);
$attributes = array_merge($this->attributes, array('created', 'updated', 'id'));
$this->data = $db_var_name->prepare_select($sql, $fields, $inputs, $attributes);

Return type: object

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;

where($field, $operator, $test, $next = NULL)

Parameters

  • field – column to be operated on
  • operator – operator to use
  • test – value to test field against
  • next (optional) default: NULL – if this is one part of a multiple part where clause, provide the next condition separator (AND and OR are examples)

Description

This function cleans up the text used for where clauses and should be used whenever using a query with a where clause in order to add a layer of security to your web applications and website.

Example(s)

$where = $db_var_name->where('col1', '=', 'abcd'); // returns WHERE col1='abcd'
$sql = "SELECT * FROM `table` {$where}";
$db_var_name->query($sql);

Deprecated in version: 1.1

Return type: string

where_prepared($field, $operator, $next = NULL)

Parameters

  • field – column to be operated against
  • operator – operator to use for comparison
  • next (optional) default: NULL – if this is one part of a multiple part where clause, provide the next condition separator (AND and OR are examples)

Description

This function cleans up the text used for where clauses for prepared statements and should be used whenever using a query with a where clause in order to add a layer of security to your web applications and website.

Example(s)

$where = $db_var_name->where_prepared('col1', '=');
$sql = "SELECT col1, col2 FROM `table` {$where};";
$fields = array(col1, col2);
$inputs = array(1234);
$result = $db_var_name->prepare_select($sql, $fields, $inputs);

Deprecated in version: 1.1

Return type: string

__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();