Update of /cvsroot/lambda/lambda/include
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26802/include
Added Files:
functions.php mysql.php
Log Message:
Added simple MySQL and error handling support.
--- NEW FILE: functions.php ---
<?php
/**
* Lambda - Online Bookmark System
* http://www.sf.net/projects/lambda
*
* Copyright (C) 2005 Ariejan de Vroom
* Licensed under the General Public License (GPL)
*
* $Id: functions.php,v 1.1 2005/07/26 13:33:00 ariejan Exp $
*/
/**
* die_error
*
* Stop execution with given error ID
**/
function die_error($id = 0)
{
global $error;
die("Lambda Error ".$id.": ".$error[$id]);
}
?>
--- NEW FILE: mysql.php ---
<?php
/**
* Lambda - Online Bookmark System
* http://www.sf.net/projects/lambda
*
* Copyright (C) 2005 Ariejan de Vroom
* Licensed under the General Public License (GPL)
*
* $Id: mysql.php,v 1.1 2005/07/26 13:33:00 ariejan Exp $
*/
/**
* Global MySQL Link identifier
**/
$db_link = null;
/**
* SQL History
**/
$sql_history = array();
/**
* db_connect
*
* Connect to the MySQL database when not yet connected.
* Returns true on success, otherwise false
**/
function db_connect()
{
global $cfg, $db_link;
if($db_link == null)
{
if($cfg['MysqlPersistent'])
{
$db_link = @mysql_pconnect($cfg['MysqlHostname'], $cfg['MysqlUsername'], $cfg['MysqlPassword'], true);
}
else
{
$db_link = @mysql_connect($cfg['MysqlHostname'], $cfg['MysqlUsername'], $cfg['MysqlPassword'], true);
}
if(@mysql_select_db($cfg['MysqlDatabase'], $db_link) == false)
{
return false;
}
else
{
return true;
}
}
else
{
// Already connected
return true;
}
}
/**
* db_query
*
* Query the database
* Make a connection when none is available
*
* $sql contains valid SQL code
*
* Return the result set or false
**/
function db_query($sql)
{
global $db_link, $sql_history;
// Verify connection
if(!$db_link && !db_connect())
{
die_error(1001);
}
$result = @mysql_query($sql, $db_link);
// Add query to sql_history and return
if($result)
{
$sql_history[] = array('sql'=>$sql, 'success'=>true);
return $result;
}
else
{
$sql_history[] = array('sql'=>$sql, 'success'=>false);
return false;
}
}
/**
* db_res2array
*
* Convert a MySQL Resource to an associative array
**/
function db_res2array($res)
{
// TODO
}
/**
* db_num_rows
*
* Return the number of rows in the MySQL Result
**/
function db_num_rows($res)
{
return @mysql_num_rows($res);
}
/**
* db_fetch_row
*
* Return the next row as an assoc array
**/
function db_fetch_row($res)
{
return @mysql_fetch_assoc($res);
}
?>
|