b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (Version 1.10) [ class tree: evocore ] [ index: evocore ] [ all elements ]

Class: DB

Source Location: /blogs/inc/_misc/_db.class.php

Class DB

Property Summary
mixed   $col_info   Column information about the last query.
string   $connection_charset  
mixed   $dbaliases   Aliases that will be replaced in queries:
object MySQL   $dbhandle  
string   $dbhost  
string   $dbname  
string   $dbpassword  
mixed   $dbreplaces   Strings that will replace the aliases in queries:
string   $dbuser  
integer|boolean   $debug_dump_function_trace_for_queries   Do we want to output a function backtrace for every query?
integer   $debug_dump_rows   Number of rows we want to dump in debug output (0 disables it)
boolean   $debug_explain_joins   Do we want to explain joins?
boolean   $error  
boolean   $halt_on_error  
mixed   $insert_id  
string   $last_error  
string   $last_query  
mixed   $last_result  
boolean   $log_queries   Do we want to log queries? This gets set according to $debug, if it's set.
integer   $num_queries  
mixed   $num_rows   Number of rows in result set (after a select)
array   $queries   Log of queries:
float   $query_duration_fast   Time in seconds that is considered a fast query (green).
float   $query_duration_slow   Time in seconds that is considered a slow query (red).
mixed   $rollback_nested_transaction   Rememeber if we have to rollback at the end of a nested transaction construct
mixed   $rows_affected   Number of rows affected by insert, delete, update or replace
boolean   $show_errors  
string   $table_options   CREATE TABLE options.
mixed   $transaction_nesting_level   How many transactions are currently nested?
mixed   $use_transactions   Use transactions in DB?

[ Top ]
Method Summary
DB   DB()   DB Constructor
void   begin()   BEGIN A TRANSCATION
void   commit()   Commit current transaction
string   compString()   Returns the appropriate string to compare $val in a WHERE clause.
string   debug_get_rows_table()   Get a table (or "<p>No Results.</p>") for the SELECT query results.
void   dump_queries()   Displays all queries that have been executed
void   escape()   Format a string correctly for safe insert under all PHP conditions
void   flush()   Kill cached query results
void   format_query()  
array   get_assoc()   Function to get the second column from the cached result indexed by the first column
array   get_col()   Function to get 1 column from the cached result set based in X index see docs for usage and info
void   get_col_info()   Function to get column meta data info pertaining to the last query see docs for more info and usage
string   get_list()   Get a column as comma-separated list.
array   get_results()   Return the the query as a result set - see docs for more details
array|object   get_row()   Get one row from the DB - see docs for more detail
mixed   get_var()   Get one variable from the DB - see docs for more detail
string   null()  
void   print_error()   Print SQL/DB error.
mixed   query()   Basic Query
string   quote()   Quote a value, either in single quotes (and escaped) or if it's NULL as 'NULL'.
void   rollback()   Rollback current transaction
void   select()   Select a DB (if another one needs to be selected)
boolean   set_connection_charset()   Set the charset of the connection.
void   week()   Returns the correct WEEK() function to get the week number for the given date.

[ Top ]
Properties
mixed   $col_info [line 118]

Column information about the last query.

Note: DB::log_queries must be enabled for this to work.

API Tags:
See:  DB::get_col_info()


[ Top ]
string   $connection_charset [line 205]
API Tags:
See:  DB::set_connection_charset()


[ Top ]
mixed   $dbaliases = array() [line 137]

Aliases that will be replaced in queries:


[ Top ]
object MySQL   $dbhandle [line 177]

[ Top ]
string   $dbhost = 'localhost' [line 199]

[ Top ]
string   $dbname [line 194]
API Tags:
See:  DB::select()


[ Top ]
string   $dbpassword [line 188]

[ Top ]
mixed   $dbreplaces = array() [line 141]

Strings that will replace the aliases in queries:


[ Top ]
string   $dbuser [line 183]

[ Top ]
integer|boolean   $debug_dump_function_trace_for_queries = 0 [line 234]

Do we want to output a function backtrace for every query?


[ Top ]
integer   $debug_dump_rows = 0 [line 240]

Number of rows we want to dump in debug output (0 disables it)


[ Top ]
boolean   $debug_explain_joins = false [line 228]

Do we want to explain joins?


[ Top ]
boolean   $error = false [line 99]

[ Top ]
boolean   $halt_on_error = true [line 95]

[ Top ]
mixed   $insert_id = 0 [line 120]

[ Top ]
string   $last_error = '' [line 111]

[ Top ]
string   $last_query = '' [line 107]

[ Top ]
mixed   $last_result [line 122]

[ Top ]
boolean   $log_queries [line 216]

Do we want to log queries? This gets set according to $debug, if it's set.

Information Tags:
Todo:  fp> get rid of this var, use $debug only

[ Top ]
integer   $num_queries = 0 [line 103]

[ Top ]
mixed   $num_rows = 0 [line 127]

Number of rows in result set (after a select)


[ Top ]
array   $queries = array() [line 222]

Log of queries:


[ Top ]
float   $query_duration_fast = 0.05 [line 247]

Time in seconds that is considered a fast query (green).

API Tags:
See:  DB::dump_queries()


[ Top ]
float   $query_duration_slow = 0.3 [line 254]

Time in seconds that is considered a slow query (red).

API Tags:
See:  DB::dump_queries()


[ Top ]
mixed   $rollback_nested_transaction = false [line 172]

Rememeber if we have to rollback at the end of a nested transaction construct


[ Top ]
mixed   $rows_affected = 0 [line 132]

Number of rows affected by insert, delete, update or replace


[ Top ]
boolean   $show_errors = true [line 91]

[ Top ]
string   $table_options = '' [line 155]

CREATE TABLE options.

This gets appended to every "CREATE TABLE" query.

Edit those if you have control over you MySQL server and want a more professional database than what is commonly offered by popular hosting providers.

Recommended settings: ' ENGINE=InnoDB ' Development settings: ' ENGINE=InnoDB DEFAULT CHARSET=utf8 '


[ Top ]
mixed   $transaction_nesting_level = 0 [line 167]

How many transactions are currently nested?


[ Top ]
mixed   $use_transactions = false [line 162]

Use transactions in DB?

You need to use InnoDB in order to enable this. See the $db_config.


[ Top ]
Methods
Constructor DB  [line 281]

  DB DB( array $params  )

DB Constructor

Connects to the server and selects a database.

Parameters:
array   $params:  An array of parameters. Manadatory:
  • 'user': username to connect with
  • 'password': password to connect with
OR
  • 'handle': a MySQL Database handle (from a previous mysql_connect())
Optional:
  • 'name': the name of the default database, see DB::select()
  • 'host': host of the database; Default: 'localhost'
  • 'show_errors': Display SQL errors? (true/false); Default: don't change member default ($show_errors)
  • 'halt_on_error': Halt on error? (true/false); Default: don't change member default ($halt_on_error)
  • 'table_options': sets $table_options
  • 'use_transactions': sets $use_transactions
  • 'aliases': Aliases for tables (array( alias => table name )); Default: no aliases.
  • 'new_link': create a new link to the DB, even if there was a mysql_connect() with the same params before. (requires PHP 4.2)
  • 'client_flags': optional settings like compression or SSL encryption. See http://www.php.net/manual/en/ref.mysql.php#mysql.client-flags. (requires PHP 4.3)


[ Top ]
begin  [line 1224]

  void begin( )

BEGIN A TRANSCATION

Note: By default, MySQL runs with autocommit mode enabled. This means that as soon as you execute a statement that updates (modifies) a table, MySQL stores the update on disk. Once you execute a BEGIN, the updates are "pending" until you execute a COMMIT or a DB:rollback()

Note 2: standard syntax would be START TRANSACTION but it's not supported by older MySQL versions whereas BEGIN is...

Note 3: The default isolation level is REPEATABLE READ.



[ Top ]
commit  [line 1238]

  void commit( )

Commit current transaction



[ Top ]
compString  [line 470]

  string compString( mixed $val  )

Returns the appropriate string to compare $val in a WHERE clause.

Parameters:
mixed   $val:  Value to create a "compare-String" for

API Tags:
Return:  Either 'IS NULL', 'IN ("a", "b", "c")' or " = 'a'".


[ Top ]
debug_get_rows_table  [line 1013]

  string debug_get_rows_table( $max_lines, [ $break_at_comma = false]  )

Get a table (or "<p>No Results.</p>") for the SELECT query results.

Parameters:
   $max_lines: 
   $break_at_comma: 

API Tags:
Return:  HTML table or "No Results" if the


[ Top ]
dump_queries  [line 1111]

  void dump_queries( )

Displays all queries that have been executed



[ Top ]
escape  [line 411]

  void escape( $str  )

Format a string correctly for safe insert under all PHP conditions

Parameters:
   $str: 


[ Top ]
flush  [line 542]

  void flush( )

Kill cached query results



[ Top ]
format_query  [line 1093]

  void format_query( $sql  )

Parameters:
   $sql: 


[ Top ]
get_assoc  [line 893]

  array get_assoc( [ $query = NULL], [ $title = '']  )

Function to get the second column from the cached result indexed by the first column

Parameters:
   $query: 
   $title: 

API Tags:
Return:  [col_0] => col_1


[ Top ]
get_col  [line 869]

  array get_col( [ $query = NULL], [ $x = 0], [ $title = '']  )

Function to get 1 column from the cached result set based in X index see docs for usage and info

Parameters:
   $query: 
   $x: 
   $title: 


[ Top ]
get_col_info  [line 984]

  void get_col_info( [ $info_type = 'name'], [ $col_offset = -1]  )

Function to get column meta data info pertaining to the last query see docs for more info and usage

Note: DB::log_queries must be enabled for this to work.

Parameters:
   $info_type: 
   $col_offset: 


[ Top ]
get_list  [line 921]

  string get_list( [string|NULL $query = NULL], [integer $x = 0], [ $title = '']  )

Get a column as comma-separated list.

Parameters:
string|NULL   $query:  Query to execute
integer   $x:  Column of the result set
   $title: 


[ Top ]
get_results  [line 932]

  array get_results( [ $query = NULL], [ $output = OBJECT], [ $title = '']  )

Return the the query as a result set - see docs for more details

Parameters:
   $query: 
   $output: 
   $title: 


[ Top ]
get_row  [line 822]

  array|object get_row( [ $query = NULL], [ $output = OBJECT], [ $y = 0], [ $title = '']  )

Get one row from the DB - see docs for more detail

Parameters:
   $query: 
   $output: 
   $y: 
   $title: 


[ Top ]
get_var  [line 791]

  mixed get_var( [ $query = NULL], [ $x = 0], [ $y = 0], [ $title = '']  )

Get one variable from the DB - see docs for more detail

Note: To be sure that you received NULL from the DB and not "no rows" check for $num_rows.

Parameters:
   $query: 
   $x: 
   $y: 
   $title: 

API Tags:
Return:  NULL if not found, the value otherwise (which may also be NULL).


[ Top ]
null  [line 434]

  string null( $val  )

Parameters:
   $val: 

API Tags:
Return:  Return the given value or 'NULL', if it's === NULL.


[ Top ]
print_error  [line 492]

  void print_error( [ $str = ''], [ $query_title = '']  )

Print SQL/DB error.

TODO: bloated: it probably doesn't make sense to display errors if we don't stop. Any use case?

Parameters:
   $str: 
   $query_title: 


[ Top ]
query  [line 558]

  mixed query( string $query, [string $title = '']  )

Basic Query

Parameters:
string   $query:  SQL query
string   $title:  title for debugging

API Tags:
Return:  # of rows affected or false if error


[ Top ]
quote  [line 422]

  string quote( $str  )

Quote a value, either in single quotes (and escaped) or if it's NULL as 'NULL'.

Parameters:
   $str: 

API Tags:
Return:  Quoted (and escaped) value or 'NULL'.


[ Top ]
rollback  [line 1265]

  void rollback( )

Rollback current transaction



[ Top ]
select  [line 394]

  void select( $db  )

Select a DB (if another one needs to be selected)

Parameters:
   $db: 


[ Top ]
set_connection_charset  [line 1296]

  boolean set_connection_charset( string $charset, [boolean $use_map = false]  )

Set the charset of the connection.

WARNING: this will fail on MySQL 3.23

Parameters:
string   $charset:  Charset
boolean   $use_map:  Use the "regular charset => mysql charset map"?

API Tags:
Return:  true on success, false on failure
Staticvar:  array $mysql_charset_map: "regular charset => mysql charset map"


[ Top ]
week  [line 452]

  void week( string $date, integer $startofweek  )

Returns the correct WEEK() function to get the week number for the given date.

Parameters:
string   $date:  will be used as is
integer   $startofweek:  0 for sunday, 1 for monday

Information Tags:
Link:  http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
Todo:  disable when MySQL < 4

[ Top ]

Documentation generated on Tue, 20 May 2008 01:39:25 +0200 by phpDocumentor 1.4.2