Connect($host,$user,$pass,$db_name)) {
return(1);
}else{
die("
Cannot connect to MySQL server on '$host'. Please make sure you have specified a valid MySQL
database name in 'include/config.php'.");
return(0);
}
}
/* db_execute - run an sql query and do not return any output
@arg $sql - the sql query to execute
@returns - '1' for success, '0' for error */
function db_execute($sql) {
global $cnn_id;
if (!$cnn_id) { db_connect(); }
$query = $cnn_id->Execute($sql);
if ($query) {
return(1);
}else{
return(0);
}
}
/* db_fetch_cell - run a 'select' sql query and return the first column of the
first row found
@arg $sql - the sql query to execute
@arg $col_name - use this column name instead of the first one
@returns - (bool) the output of the sql query as a single variable */
function db_fetch_cell($sql,$col_name = '') {
global $cnn_id;
if (!$cnn_id) { db_connect(); }
if ($col_name != '') {
$cnn_id->SetFetchMode(ADODB_FETCH_ASSOC);
}else{
$cnn_id->SetFetchMode(ADODB_FETCH_NUM);
}
$query = $cnn_id->Execute($sql);
if ($query) {
if (!$query->EOF) {
if ($col_name != '') {
return($query->fields[$col_name]);
}else{
return($query->fields[0]);
}
}
}
}
/* db_fetch_row - run a 'select' sql query and return the first row found
@arg $sql - the sql query to execute
@returns - the first row of the result as a hash */
function db_fetch_row($sql) {
global $cnn_id;
if (!$cnn_id) { db_connect(); }
$cnn_id->SetFetchMode(ADODB_FETCH_ASSOC);
$query = $cnn_id->Execute($sql);
if ($query) {
if (!$query->EOF) {
return($query->fields);
}
}
}
/* db_fetch_assoc - run a 'select' sql query and return all rows found
@arg $sql - the sql query to execute
@returns - the entire result set as a multi-dimensional hash */
function db_fetch_assoc($sql) {
global $cnn_id;
if (!$cnn_id) { db_connect(); }
$data = array();
$cnn_id->SetFetchMode(ADODB_FETCH_ASSOC);
$query = $cnn_id->Execute($sql);
if ($query) {
while ((!$query->EOF) && ($query)) {
$data{sizeof($data)} = $query->fields;
$query->MoveNext();
}
return($data);
}
}
/* db_fetch_insert_id - get the last insert_id or auto incriment
@returns - the id of the last auto incriment row that was created */
function db_fetch_insert_id() {
global $cnn_id;
return $cnn_id->Insert_ID();
}
/* array_to_sql_or - loops through a single dimentional array and converts each
item to a string that can be used in the OR portion of an sql query in the
following form:
column=item1 OR column=item2 OR column=item2 ...
@arg $array - the array to convert
@arg $sql_column - the column to set each item in the array equal to
@returns - a string that can be placed in a SQL OR statement */
function array_to_sql_or($array, $sql_column) {
/* if the last item is null; pop it off */
if ((empty($array{count($array)-1})) && (sizeof($array) > 1)) {
array_pop($array);
}
if (count($array) > 0) {
$sql_or = "(";
for ($i=0;($i value mapping in the row
@arg $keyCols - the name of the column containing the primary key
@arg $autoQuote - whether to use intelligent quoting or not
@returns - the auto incriment id column (if applicable) */
function db_replace($table_name, $array_items, $keyCols) {
global $cnn_id;
$cnn_id->Replace($table_name, $array_items, $keyCols);
return $cnn_id->Insert_ID();
}
/* sql_save - saves data to an sql table
@arg $array_items - an array containing each column -> value mapping in the row
@arg $table_name - the name of the table to make the replacement in
@arg $key_cols - the primary key(s)
@returns - the auto incriment id column (if applicable) */
function sql_save($array_items, $table_name, $key_cols='id') {
global $cnn_id;
while (list ($key, $value) = each ($array_items)) {
if (preg_match("/^((password|md5|now)\(.*\)|null)$/i", $value)) {
$quote = "";
}else{
$quote = "\"";
}
$array_items[$key] = "$quote$value$quote";
}
if (!$cnn_id->Replace($table_name, $array_items, $key_cols, false)) { return 0; }
/* get the last AUTO_ID and return it */
if ($cnn_id->Insert_ID() == "0") {
if (isset($array_items["id"])) {
return str_replace("\"", "", $array_items["id"]);
}else{
return 0;
}
}else{
return $cnn_id->Insert_ID();
}
}
?>