auDB

From auWiki
Jump to: navigation, search

The auDB class is part of the auLib package. Its purpose is to connect to and interact with a database. It is currently available for MySQL only, but the same interface can likely be used for other databases as well.

Usage

Create an auDB object using new auDB(), then run queries against it with Get(), GetRecord(), GetValue(), GetLimit(), GetSplit(), Put(), and Change(). NumQueries() will tell you how many queries have been run since the object was created.

Public Functions

new auDB

new auDB($user, $pass, $host, $name, $report)

Creates a new database object.

  • $user = Database account username.
  • $pass = Database account password.
  • $host = Name of server hosting the database (often 'localhost' works).
  • $name = Name of database to work with.
  • $report = Object with Error($, $) and Info($) methods for displaying error and information messages. Defaults to built-in object which simply writes HTML messages.

SetReport

SetReport($report)

Sets the object to be used for reporting errors and information.

  • $report = Object with Error($, $) and Info($) methods for displaying error and information messages. Defaults to built-in object which simply writes HTML messages.
  • @return = True if $report had the required methods and is going to be used.

NumQueries

NumQueries()

Gets the number of queries run against the database using this object.

  • @return = Number of queries run against the database.

Get

Get($query, $errormsg, $emptymsg, $emptyerror)

Runs an SQL SELECT query against the database.

  • $query = SQL SELECT query to run.
  • $errormsg = Error message to report if the query results in an error. Pass an empty string to return false but not report a message. Default is to return the results without checking for errors.
  • $emptymsg = Message to report if the query does not return any results. Pass an empty string to return false but not report a message. Default is to return the results without checking to see if there are any results.
  • $emptyerror = Whether empty results should be reported as an error. Default is to report empty results as information.
  • @return = The results as an auDBResult object, or false.

GetRecord

GetRecord($query, $errormsg, $emptymsg, $emptyerror)

Runs an SQL SELECT query against the database and gets back just the first record of the results.

  • $query = SQL SELECT query to run.
  • $errormsg = Error message to report if the query results in an error. Pass an empty string to return false but not report a message. Default is to return the results without checking for errors.
  • $emptymsg = Message to report if the query does not return any results. Pass an empty string to return false but not report a message. Default is to return the results without checking to see if there are any results.
  • $emptyerror = Whether empty results should be reported as an error. Default is to report empty results as information.
  • @return = The first record of the results as an object, or false.

GetValue

GetValue($query, $errormsg, $emptymsg, $emptyerror)

Runs an SQL SELECT query against the database and gets back just the first value from the first record of the results.

  • $query = SQL SELECT query to run.
  • $errormsg = Error message to report if the query results in an error. Default is to not write an error message.
  • $emptymsg = Message to report if the query does not return any results. Default is to not write a message.
  • $emptyerror = Whether empty results should be reported as an error. Default is to report empty results as information.
  • @return = The first value from the first record of the results, or false.

GetLimit

($query, $skip, $show, $errormsg, $emptymsg, $emptyerror)

Runs an SQL SELECT query against the database and gets back a limited number of records in the results.

  • $query = SQL SELECT query to run.
  • $skip = Number of result records to skip.
  • $show = Number of result records to show.
  • $errormsg = Error message to report if the query results in an error. Pass an empty string to return false but not report a message. Default is to return the results without checking for errors.
  • $emptymsg = Message to report if the query does not return any results. Pass an empty string to return false but not report a message. Default is to return the results without checking to see if there are any results.
  • $emptyerror = Whether empty results should be reported as an error. Default is to report empty results as information.
  • @return = The results as an auDBResult object, or false.

GetSplit

GetSplit($query, $defshow, $defskip, $skip, $show, $errormsg, $emptymsg, $emptyerror, $numrecords)

Runs an SQL SELECT query against the database and gets back a page of results.

  • $query = SQL SELECT query to run.
  • $defshow = Default number of records to return.
  • $defskip = Default number of records to skip. Default is 0.
  • $skip = Index to $_GET to use for 'skip.' Default is 'skip'.
  • $show = Index to $_GET to use for 'show.' Default is 'show'.
  • $errormsg = Error message to report if the query results in an error. Pass an empty string to return false but not report a message. Default is to return the results without checking for errors.
  • $emptymsg = Message to report if the query does not return any results. Pass an empty string to return false but not report a message. Default is to return the results without checking to see if there are any results.
  • $emptyerror = Whether empty results should be reported as an error. Default is to report empty results as information.
  • $numrecords = If true, use NumRecords() to get count.
  • @return = The results as an auDBResult object, or false.

SplitPageLinks

SplitPageLinks($get, $indent)

Write out links to other pages of a split query.

  • $get = Beginning of the query string that page variables will be added onto. Default is '?' which means no additional variables. This value should always start with a question mark. If there are other query string variables, the value should end with & so page variables can be added directly.
  • $indent = Base indentation level -- a number of spaces or tabs. Default is 6 spaces, which should mean 3 levels of indentation.

Put

Put($query, $errormsg)

Runs an SQL INSERT or REPLACE query against the database. Note that it will sometimes return an empty string (or maybe 0) and sometimes return false, and they have different meanings.

  • $query = SQL INSERT/REPLACE query to run.
  • $errormsg = Error message to report if the query results in an error. Pass an empty string to return false but not report a message. Default is to return the results without checking for errors.
  • @return = The ID of the inserted record, or false on failure.

Change

Change($query, $errormsg, $emptymsg, $emptyerror)

Runs an SQL UPDATE or DELETE query against the database. Note that it will sometimes return 0 and sometimes return false, and they have different meanings.

  • $query = SQL UPDATE/DELETE query to run.
  • $errormsg = Error message to report if the query results in an error. Pass an empty string to return false but not report a message. Default is to return the results without checking for errors.
  • $emptymsg = Message to report if the query does not affect any records. Pass an empty string to return false but not report a message. Default is to return the number of records without checking it.
  • $emptyerror = Whether affecting no records should be reported as an error. Default is to report as information.
  • @return = The number of records affected, or false.