default
[ class tree: default ] [ index: default ] [ all elements ]

Procedural File: datalib.php

Source Location: /datalib.php



Page Details:



Includes:

require_once(__FILE__) [line 12]
This file defines a number of helper functions that make it easier to query, insert, update or delete database records.

It uses the PDO classes provided with PHP 5.x as the low level database abstraction layer. Prior to calling any of the functions in this file, you must establish a database connection and store the connection in a global variable named

  1. $db
.








build_filter [line 83]

void build_filter( array $filter, &$select &$values, &$values 2)

Converts name/values pairs into an an SQL where clause.



Parameters

&$values   2   References an array to hold the parameters.
array   $filter   Array of name/value pairs.
&$select   &$values   References a string to hold the query.
[ Top ]



count_records [line 112]

integer count_records( string $table, [array $filter = ""])

Returns the number of records that match the conditions.

Parameters:




Tags:

return:  Number of records that match the filter conditions.


Parameters

string   $table   The name of the database table
array   $filter   An array of field name/value pairs.
[ Top ]



delete_records [line 269]

boolean delete_records( string $table, array $filter)

Deletes one or more database records



Tags:

return:  True indicates success


Parameters

string   $table   The name of the database table
array   $filter   An array of field name/value pairs
[ Top ]



execute_sql [line 41]

any execute_sql( string $command, [boolean $wantResult = false], [boolean $feedback = false], [boolean $dieOnError = false])

Executes the provided SQL command and optionally returns the results.



Tags:

return:  Returns true/false to indicate success/failure or the results if
  1. $wantResult
is true


Parameters

string   $command   The SQL command to execute
boolean   $wantResult   Flag to return the results
boolean   $feedback   Flag to show result to user
boolean   $dieOnError   Flag to abort script on failure
[ Top ]



get_field [line 217]

any get_field( string $table, $return, $return $filter)

Get a single field from a record.



Tags:

return:  The value on success,
  1. null
otherwise.


Parameters

string   $table   The name of the database table
$return   $filter   The field/column value to return.
   $return  
[ Top ]



get_record [line 143]

object The get_record( string $table, array $filter)

Lookups a record using a filter and returns it as a standard object. If more than one record matches, only the first is returned.



Tags:

return:  first matching record, or false if not found.


Parameters

string   $table   The name of the database table
array   $filter   An array of field name/value pairs.
[ Top ]



get_records [line 182]

object The get_records( string $table, [array $filter = ""], [string $sort = ""], [string $fields = "*"], [integer $limitfrom = 0], [integer $limitcount = 0])

Gets zero or many records from a table as an array of objects with optional filtering, sorting, list of fields and limits.

NOTE: This function can be more efficient by using the native database LIMIT/OFFSET options, however they are not portable and the details differ from one database server to another. This function will use a lot of memory on large datasets.




Tags:

return:  first matching record, or false if not found.


Parameters

string   $table   The name of the database table
array   $filter   An array of field name/value pairs.
string   $sort   The optional sort sequence.
string   $fields   An optional list of record fields.
integer   $limitfrom   An optional offset to the first record.
integer   $limitcount   An optional limit on number of records returned.
[ Top ]



insert_record [line 322]

boolean insert_record( string $table, object $dataobject, [boolean $returnid = false])

Creates a new database record from an object.



Tags:

return:  Return
  1. true
on success, unless
  1. $returnid
is true, then returns the new ID value.


Parameters

string   $table   The name of the database table
object   $dataobject   A standard object holding the record values
boolean   $returnid   Flag to return the inserted record's primary key
[ Top ]



record_exists [line 71]

boolean record_exists( string $table, array $filter)

Determine if a record matching up to 3 filter conditions exists or not.



Tags:

return:  True indicates one or more records exist


Parameters

string   $table   The database table/view name
array   $filter   An array of field name/value pairs.
[ Top ]



reportError [line 20]

void reportError( PDOException $error)

Record a PDO error message to the log file.



Parameters

PDOException   $error   A PDO exception object.
[ Top ]



set_field [line 245]

boolean set_field( string $table, string $setfield, any $newvalue, array $filter)

Updates one field in a database record.



Tags:

return:  True indicates success


Parameters

string   $table   The name of the database table
string   $setfield   The field in the record to change
any   $newvalue   The field's new value
array   $filter   An array of field name/value pairs
[ Top ]



table_info [line 295]

array table_info( string $table)

Helper function to retrieve information about a table's columns.



Tags:

return:  An array of column metadata (See PDO documentation.)


Parameters

string   $table   Name of the table in the database.
[ Top ]



update_record [line 374]

boolean update_record( string $table, object $dataobject, [string $primarykey = "id"])

Issues an UPDATE statement with modified record values.



Tags:

return:  Indicates success or failure.


Parameters

string   $table   Name of the database table.
object   $dataobject   A PHP object with the new record values.
string   $primarykey   The name of the primary key column.
[ Top ]



Documentation generated on Wed, 15 Nov 2006 08:48:03 -0600 by phpDocumentor 1.3.1