![]() |
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HOME | WEB DESIGN | COMPUTING | GRAPHICS/PHOTOS | MUSIC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Web Sites
Web Design
Web Technology
Panoramic Earth
School Web Sites
Summer Web Sites
DLib PHP Library
DLib PHP Library API
database.php
datetime.php
dbautofuncs.php
dbclasses.php
dbdefines.php
dbrecord.php
dbscreens.php
dbsync.php
dbsync1.php
dbsync2.php
dlibinfo.php
ecommerce.php
formfields.php
ftp.php
functions.php
graphical.php
html.php
htmldefines.php
htmlpage.php
lib.php
numerical.php
pdfgenlib.php
protx.php
session.php
system.php
tables.php
|
![]() |
database.phpDLib PHP Library APIGeneral database functionsThis file contains all the general purpose generic functions for handling databases such as creating, opening and closing databases, general SQL functions, table update functions and other miscellaneous functions. See dbclasses.php and dbrecord.php for the OO database classes and dbautofuncs.php for useful functions that are generated on-the-fly when record classes are created. Also, see the various db_xxxx.php files for database-type specific implementations (e.g. db_mysql.php or db_access.php).The following code shows how a database connection is created, switched to and then opened. Records from a Stock Item table are then read (see dbclasses.php for how to define record layouts). EXAMPLE
$con1 = newDbConnection ("mysql", "localhost", "dblocal", "dbuser",
"abcd123", "root", "rootpswd");
switchDatabase ($con1);
if (dbConnect ())
{
list ($ok, $stkList) = readStockItems ("cost < 0.50");
foreach ($stkList as $stk)
print "$stk->code - $stk->description - $stk->price\n";
dbClose ();
}
Class/Function/Method List
File Version: 5.4.4 - 5 Jun, 2008
Adds another bit of SQL statement to make up a WHERE statement for SQL. Similar to addWhere except that it does not assumes the presence of a global $where variable. Result: Returns the new $where string.
Add a table definition to the current settings. Creates a global TableDef variable called $tdXXX where XXX is $className. Optionally creates the default standard database functions associated with that class (see dbautofuncs.php). See dbclasses.php for the TableDef class.
EXAMPLE Add a table called stockitems to the current table definitions and associate it with the
StockItem record type. Allow the system to create the Auto Functions (see dbautofuncs.php)
so that we can call functions such as readStockItems and countStockItems without actually
needing to create them manually.
addTable ("stockitems", "StockItem", true);
Adds another bit of SQL statement to make up a WHERE statement for SQL - assumes the presence of a global $where variable being used by the calling code.
EXAMPLE We have a database table containing contact names and addresses and $name and $town have been
passed in from a search form. This code will then populate the WHERE statement ready to be
sent to an SQL read function (in this case readContacts).
$where = "";
if (!empty ($name))
addWhere ("name LIKE '%$name%'");
if (!empty ($town))
addWhere ("town LIKE '%$town%'");
list ($ok, $recList) = readContacts ($where, "name");
Create all tables using data in $_db->tables (from classes that extend (Ex)DbRecord). Note that only table allowed in the currently enabled database will be created. Close the currently selected database. To close multiple databases then use switchDatabase to select a db and then close it using this function. Result: True if database successfully closed. Connect to the specified database optionally creating the database if allowed. If we don't find any records in the systemval table then attempt to do a create on all the tables. Result: True if the connect succeeded. EXAMPLE Set up a database connection, switch to it and then try to connect.
$con1 = newDbConnection ("mysql", "localhost", "dblocal", "dbuser", "abcd123");
switchDatabase ($con1);
if (dbConnect ())
{
// Connection succeeded!
// Do some database access here...
}
Database message handler. This accumulates database errors into one string for later display.
Check to see if a database update is required. This is performed either once a day, once an hour or every time this function is called (depending upon the setting of $_db->dbChanges). Execute an SQL statement. If it fails then it checks for record layout changes and, if found, attempts to update the table (except for SystemVal). Result: Returns true if SQL succeeded.
EXAMPLE
if (doSQL ("UPDATE stockitems SET price = 10.23 WHERE ref = 'A2-DEX'"))
print "Update succeeded";
Return the DbField info about the specified field in the specified table. Result: Returns a two element array: 0 = a boolean success indicator; 1 = an (Ex)DbField class record containing the field details.
Return the current database trace as a string. Result: String containing a formatted copy of the current DB trace.
Set up a database connection's details. Any number of connections can be set up and then switched between using the switchDatabase function. Result: Connection number.
EXAMPLE Set up connection 1 to a local MySQL database and control over database creation (by sending
the root user name and password), and connection 2 to a remote MS Access database where
we also do not have control over database creation.
$con1 = newDbConnection ("mysql", "localhost", "dblocal", "dbuser",
"abcd123", "root", "rootpswd");
$con2 = newDbConnection ("access", "www.banana.org", "dbxyz",
"dbuser", "defg987");
Read from multiple tables. Result: Element 0 is either true or false denoting the success or otherwise of the call. Element 1 is an array of objects where each object itself an array of the specified class records.
EXAMPLE Read the businesses and associated addresses and print them out.
list ($ok, $recList) = readFromMulti ("Business,Address", "ref",
"t1.town LIKE '%Hampstead%', "t0.name");
foreach ($recList as $rec)
{
list ($business, $address) = $rec;
print $business->name . "\n" . $address->addr1 . "\n" .
$address->town;
}
Remove the last entry from dbMsg. Used where an error occurs but is then handled properly by the calling code.
Change which database we are currently accessing.
EXAMPLE Assuming that $con1 and $con2 have been set up as connections to two separate databases and we have
also performed a successful dbConnect call to both of them then we can switch between them.
This example reads some records from one database and then saves them to a second before switching
back to the original database. Note that we check for the record already existing in the second
database and, if it is NOT found, then we set newRec - internally, this tells the system that
this is a new record which creates the correct SQL to do an INSERT instead of an UPDATE statement.
switchDatabase ($con1);
list ($ok, $recList) = readStockItems ("cost < 0.50");
if ($ok)
{
switchDatabase ($con2);
foreach ($recList as $rec)
{
$rec2 = new StockItem ($rec->ref);
if (!$rec2->ok)
$rec->newRec = true;
$rec->save ();
}
switchDatabase ($con1);
}
Get table entry from the table or class name name. Result: Returns an array of 3 items: 0 = boolean success indicator; 1 = the associated (Ex)TableDef class; 2 = a blank instance of the associated record class.
Begin a transaction. Note that these Transaction functions can only be used on tables that can handle transactions (e.g. InnoDB, BDB etc for MySQL). Result: Success value. Commit (end) a transaction. Result: Success value. End (commit) a transaction - alternate name. Result: Success value. Roll back a transaction. Result: Success value. Update a table definition. This compares the stored record layout with the DbField entries for that record and, if differences are detected, then it attempts to automatically update the database table to bring them into sync.
Checks to see if any tables need updating (calls updateTable for all tables defined). |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| copyright ©2003-2008 davidviner.com | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||