Here's an example of the most often used database functions:
<?php
$result = db_fetch (
'select * from sitellite_mime_type'
);
info ($result);
// result will be one of the following:
// - false if the query failed or there were no results (check db_error() to differentiate)
// - an object if there was only one result
// - an array of objects if there was more than one result
$result = db_fetch_array (
'select * from sitellite_mime_type'
);
info ($result);
// result is always an array of objects (or an empty array)
$result = db_shift (
'select title from sitellite_news where id = ?',
$parameters['id']
);
info ($result);
// result is the value of the first field of the first search result
$result = db_single (
'select * from sitellite_news where id = ?',
$parameters['id']
);
info ($result);
// result is an object
$result = db_shift_array (
'select id from sitellite_news'
);
info ($result);
// result is a numeric array of news story ids
$result = db_pairs (
'select id, title from sitellite_news'
);
info ($result);
// result is an associative array with id as the key and title as the value for each entry
$result = db_execute (
'insert into fake_table values (1, 2, 3)'
);
// result is a true/false value determining the
// success of the statement executed
$result = db_lastid ();
// result is the last auto-incremented value from a
// previous call to db_execute()
$quoted = db_quote ('text to quote');
// escapes text for inclusion in an SQL statement
// this is done automatically for placeholders
// (question mark substitution).
$error_message = db_error ();
$error_number = db_errno ();
$query = db_query (
'select * from sitellite_news where category = ?'
);
// query is an saf.Database.Query object -- see:
// http://www.sitellite.org/docs/Database/Query.html
// example usage:
if ($query->execute ($parameters['category'])) {
$total = $query->rows ();
$results = array ();
while ($result = $query->fetch ()) {
$results[] = $result;
}
$q->free ();
} else {
die ($query->error ());
}
?>
Note that these are aliases of functions you can call on a global $db object, but they're much easier to type and keep code more readable as functions.
For more info about the saf.Database package, see:
Revised on June 7, 2005 9:13 PM by admin
Back in time (1 more) | Linked from: Libraries