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();
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.
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();
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
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
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
Parameters
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
Parameters
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
Parameters
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
Parameters
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;
Parameters
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
Parameters
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
Parameters
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();