Constructor function
CRUDites
__construct
([string $dbuser = ''], [string $dbpassword = ''], [string $dbname = ''], [string $dbhost = 'localhost'], [string $prefix = false])
-
string
$dbuser: Database username
-
string
$dbpassword: Database password
-
string
$dbname: Database name
-
string
$dbhost: (Optional) Database host, defaults to localhost
-
string
$prefix: (Optional) Tables' prefix, defaults to none
Adds a table to the internal table reference
Example:
'#__my_table',
array(
'id',
'name',
'surname'
)
);
void
add_table
(string $table, [array $fields = null], [string $primary = 'id'], [bool $override = false])
-
string
$table: Table name (with or without prefix)
-
array
$fields: An array containing all table fields
-
string
$primary: (Optional) Table primary key (defaults to 'id')
-
bool
$override: (Optional) If set to true any already added table with the same name will be overwritten. Defaults to false
Same as add_table(), but accepts as first arguments a multidimensional associative array with tables' names as keys and field array as value.
Primary key can be marked inside the field array as a associative key __primary => 'fieldname'
Example:
'#__my_table' => array (
'id',
'name',
'surname',
'__primary' => 'id'
),
'#__my_second_table' => array (
[...]
)
));
void
add_tables
([array $array = array()], [bool $override = false])
-
array
$array: The multidimensional associative array of tables to insert
-
bool
$override: (Optional) If set to true any already added table with the same name will be overwritten. Defaults to false.
Converts an array to a string with parameters, by combining keys and values.
Values which are arrays may be imploded as a comma separated string
Inspired by Joomla 1.5 Framework
string
array_to_string
([array $array = null], [ $inner_glue = '='], [ $outer_glue = ' AND '], [bool $recurse = true], string $innerglue, string $outerglue)
-
array
$array: Input array
-
string
$innerglue: (Optional) String to place between each key and value. Defaults to "="
-
string
$outerglue: (Optional) String to place between each key/value pair. Defaults to " AND "
-
bool
$recurse: (Optional) If set to true it will implode array values which are arrays in a comma separated string. Defaults to true
-
$inner_glue
-
$outer_glue
Selects a table from the internal table reference store to be use with binding methods, if it exists
Example:
$db->bind('#__my_table');
void
bind
(strin $table)
-
strin
$table: Name of selected table
Returns either an update or insert query based on param $type
string
build_query
(string $table, array $value_array, string $type, mixed $where, [bool $compat = false], bool $all)
-
string
$table: Table name
-
array
$value_array: An array where keys are table fields' names and values are fields' values to update
-
string
$type: Type of query. Allowed values are update or insert
-
mixed
$where: (Not needed if type is insert) Conditional string or array ('fieldname'=>'fieldvalue') to define rows to be updated
-
bool
$all: (Optional. Not needed if type is insert) Uses a compatibility routine with old versions of CRUDites
-
bool
$compat: (Optional) Uses a compatibility routine with old versions of CRUDites
Checks if a string is correctly escaped and quoted for SQL queries
mixed
check_quotes
(mixed $var)
-
mixed
$var: A variable to check
Counts the number of rows from bound table
int
count
([string|array $where = ''])
-
string|array
$where: (Optional) where statement
Counts number of rows with optional where statement
int
count_rows
([string $table = null], [string|array $where = ''])
-
string
$table: Table name
-
string|array
$where: (Optional) Where statement
Deletes a row from bound table
bool
delete
([string|array $where = ''], [ $col = null], string $table)
-
string
$table: Table name
-
string|array
$where: (Optional) Where statement as string or as an array of field => fieldvalue
-
$col
Deletes a row from passed in table
bool
delete_from
(string $table, [string|array $where = ''])
-
string
$table: Table name
-
string|array
$where: (Optional) Where statement as string or as an array of field => fieldvalue
Returns true or number of occurrence of a row if it exists
bool|int
exists
([string $val = ''], [string $col = null], [string $return = false])
-
string
$val: Value to search for
-
string
$col: (Optional) Field to use for search. Defaults to primary key of bound table
-
string
$return: (Optional) If set to true will return the number of occurrence of the searched values, else will return just true (defaults to false)
Takes a multidimensional array and returns an array with one value as key and another as value
Example:
$inputArray = array(
0 => array('one','two','three'),
1 => array('four','five','six')
);
Array (
'one' => 'two',
'four' => 'five'
)
array
flat_array
(array $array, [mixed $key1 = 0], [mixed $key2 = 1])
-
array
$array: Input Array
-
mixed
$key1: (Optional) Key of elements to use as keys. Defaults to 0
-
mixed
$key2: (Optional) Key of elements to use as values. Defaults to 1
Returns an array of values from a single column
array|bool
get_col_values
([string $table = null], string $field)
-
string
$table: Table name
-
string
$field: Table column to return
Returns an array of database rows, either in object or array format
array|bool
get_data
([array $queryArray = array()], [constant $output = OBJECT])
-
array
$queryArray: An array containing required statements
-
constant
$output: (Optional) Type of rows in the list. May be OBJECT,ARRAY_N (array with numeric index) or ARRAY_A (associative array). Defaults to OBJECT
Gets an option value
mixed
get_option
(string $var)
Returns an array with two given database columns as array keys and values
Example:
$results =
$db->get_pair("my_table","id","name","surname='Doe'")
Array (
1 => 'John',
2 => 'Jack',
4 => ...
)
array
get_pair
(string $table, string $key1, string $key2, [string $where = ""])
-
string
$table: Table name
-
string
$key1: Column name to use as key in the resulting array
-
string
$key2: Column name to use as value in the resulting array
-
string
$where: (Optional) Condition to filter results
Returns an array with all rows and colums from a give table
array|bool
get_table
([string $table = null], [constant $output = OBJECT])
-
string
$table: Table to use
-
constant
$output: (Optional) Type of rows in the list. May be OBJECT,ARRAY_N (array with numeric index) or ARRAY_A (associative array). Defaults to OBJECT
Gets a single value from a single row from a table
mixed
get_value
([string $table = null], string $field, [string|array $where = null])
-
string
$table: Table name
-
string
$field: Field to return
-
string|array
$where: (Optional) Where statement as string or as an array of field => fieldvalue
Returns an array of database rows
Example:
$results =
$db->get_where("name","my_table","surname='Doe'");
//will output an array of objects
//to get all columns use this syntax
$results =
$db->get_where("my_table","surname='Doe'");
Works like get_data() but accepts a variable number of strin arguments
array|bool
get_where
(mixed $string[,)
-
mixed
$string[,: $string2...] Select statements. May be what to select, from which table and where, or just from which table and where. An additional last argument may be the output format
String safe version of PHP implode function
string
implode
(string $glue, array $array)
-
string
$glue: String to use to join array elements
-
array
$array: Input array
Returns an insert query
string
insert_query
(string $table, array $value_array, [bool $compat = false])
-
string
$table: Table name
-
array
$value_array: An array where keys are table fields' names and values are fields' values to store
-
bool
$compat: (Optional) Uses a compatibility routine with old versions of CRUDites
Inserts a row in the bound table only if there's no other row with passed in value in current primary key
bool
insert_unique
(string $value, string $query)
-
string
$value: Value to search for in primary key
-
string
$query: Insert query
Inserts a row in the table only if there's no other row with passed in column value
bool
insert_unique_to
(string $table, string|array $field, string $value, string $query)
-
string
$table: Table name
-
string|array
$field: Field to use for search or array with field => fieldvalue
-
string
$value: (Optional) Value to search for if $field is a string
-
string
$query: Insert query
Returns current date in a MySQL DATE formatted string (yyyy-mm-dd)
string
mysql_curdate
()
Formats a formatted date or datetime to MySQL date or datetime
string
mysql_date_format
([string $string = null], [string $input = null], [string $output = null])
-
string
$string: String to format
-
string
$input: Input format. May be a default locale (date,time or datetime) or a strptime allowed format strptime
-
string
$output: MySQL output format. May be mysql_date, mysql_time, mysql_datetime or any date allowed format date
Returns current date and time in a MySQL DATE formatted string (yyyy-mm-dd 00:00:00)
string
mysql_now
()
Returns an array with two given table columns as array keys and values
array
pair
(string $key1, string $key2, [string $where = ""])
-
string
$key1: Column name to use as key in the resulting array
-
string
$key2: Column name to use as value in the resulting array
-
string
$where: (Optional) Condition to filter results
Returns true or number of occurrence of a row if it exists
bool|int
row_exists
([string $table = null], string|array $field, [string $value = ""], [string $return = false])
-
string
$table: Table name
-
string|array
$field: Field to use for search or array with field => fieldvalue
-
string
$value: (Optional) Value to search for if $field is a string
-
string
$return: (Optional) If set to true will return the number of occurrence of the searched values, else will return just true (defaults to false)
Returns a select query with options
Example:
Available statements (could be in random order)
'select' => 'id,name,surname',
'from' => 'customer_names',
'where' => 'id >= 1'
));
'select' => 'id,surname',
'from' => 'customer_names',
'where' => array('name'=>'Bob','city' => 'London'),
'order' => 'name ASC,id ASC ',
'limit' => 2 //just two rows
));
If select statement is not specified all columns will be retrieved
string
select_query
([array $array = array()])
-
array
$array: An array containing required statements
Returns an array of database rows from bound table
If no params are passed in all rows from the bound table are returned as an array of objects
array|bool
select_rows
([array $array = array()], [constant $output = OBJECT])
-
array
$array: (Optional) An array containing select statements
-
constant
$output: (Optional) Type of rows in the list. May be OBJECT,ARRAY_N (array with numeric index) or ARRAY_A (associative array). Defaults to OBJECT
Submits passed in query string to the database
bool
send_query
(string $query, [bool $isupdate = false])
-
string
$query: Query to submit
-
bool
$isupdate: (Optional) Set this parameter to true if query is an update query. Defaults to false
Sets an option
void
set_option
(string $var, string $value)
-
string
$var: Option name
-
string
$value: Value of the option
Sets value of a single field in a table
bool
set_value
([string $table = null], string $field, string $value, [string|array $where = null])
-
string
$table: Table name
-
string
$field: Field to return
-
string
$value: Value to set
-
string|array
$where: (Optional) Where statement as string or as an array of field => fieldvalue
Stores data in the bound table, either inserting a new row or updateing an existing one.
If in passed in data array is present a key with same name as the current primary key, row is updated, else a new row will be stored
Example:
// add a new table and bind the object to it
array(
'id',
'name'
),
'id' //id is the primary key
);
$db->bind('#__my_table');
// adds a new row
$new_row = array('name'=>'John');
// update a row
$new_row = array('id'=>2,'name'=>'Mark');
bool
store
(array $value_array)
-
array
$value_array: An array containing fields as keys and fields value as values
Converts and array generated by strptime to a timestamp
string
strptime_to_timestamp
([array $array = null])
-
array
$array: (Optional) Input array, if none is pass current timestamp will be returned
Gets a table name by applying prefix placeholder (#__) subsitution if needed
string
table_name
([string $table = ''])
-
string
$table: Table name
Converts a passed in date/time string to timestamp
string
to_timestamp
(string $string, string $format)
-
string
$string: Input string
-
string
$format: Input string format. May be a default locale (date,time or datetime) or a strptime allowed format strptime
Returns an update query
string
update_query
(string $table, array $value_array, mixed $where, [bool $all = false], [bool $compat = false])
-
string
$table: Table name
-
array
$value_array: An array where keys are table fields' names and values are fields' values to update
-
mixed
$where: Conditional string or array ('fieldname'=>'fieldvalue') to define rows to be updated
-
bool
$all: (Optional) Uses a compatibility routine with old versions of CRUDites
-
bool
$compat: (Optional) Uses a compatibility routine with old versions of CRUDites