Location: PHPKode > projects > Study planner > splanner_015/db/mysql/db.inc.php
<?php  
/**
 * Database functions for the MySQL database engine.
 *
 * $Author: jebbol $
 * $Revision: 1.10 $
 * $Date: 2006/08/21 19:41:50 $
 * 
 * @copyright Copyright (c) 2005 Studiewijzer.nl 
 * @link      http://www.studiewijzer.net
 * @license   http://www.gnu.org/copyleft/gpl.html
 */


// Human language translations
require_once ($SPL_CFG['dirroot'].'/lang/'.$_SESSION['usp_lang'].'/db.lang.php');

$dbhandle = @mysql_connect($SPL_CFG['dbhost'], $SPL_CFG['dbuser'], $SPL_CFG['dbpass']);
@mysql_select_db($SPL_CFG['dbname'], $dbhandle) or die(isset ($SPL_TXT['db_cannot_open']) ? $SPL_TXT['db_cannot_open'] : mysql_error());

//=========================================================================
// Public functions
//=========================================================================

/** 
 * Closes a database connection
 *
 * @param  handle $hConnection (optional)
 * @return boolean TRUE(succeeded) OR FALSE
 * @access public
 */
function db_close($connection='default'){

  global $SPL_CFG,$dbhandle;

  if (($connection=='default') && (isset($dbhandle))){ 
    @mysql_close ($dbhandle);
  } else {
    @mysql_close ($connection);
  }
  $dbhandle = null;
  return (bool)true;
}

/** 
 * Moves the pointer within teh recordset to the given row number
 *
 * @param  recordset $rs 
 * @param  integer   $row
 * @return Boolean   success or failure
 * @access public
 */
function db_data_seek($rs, $row){
  return mysql_data_seek($rs, $row);
}

/** Returns a list of enum values for a given database column
 *
 * @param  string $table The name of the table
 * @param  string $field The name of the column
 * @return string        A list with enum values
 */
function db_enum_values($table,$field){

  $result = @mysql_query("DESCRIBE $table");

  while($row = @mysql_fetch_array($result)){
    $fieldTypeSplit='';
    ereg('^([^ (]+)(\((.+)\))?([ ](.+))?$',$row['Type'],$fieldTypeSplit);
    //split type up into array
    $fieldType = $fieldTypeSplit[1];
    $fieldLen = $fieldTypeSplit[3];
    if ( ($fieldType=='enum' || $fieldType=='set') && ($row['Field']==$field) ){
      $fieldOptions = split("','",substr($fieldLen,1,-1));
      return $fieldOptions;
    }
  }
  return (bool) false;
}

/** 
 * Prepares a string for safe use in a database query
 *
 * @param  string $sUnescaped the original string
 * @return string the query string
 */
function db_escape_string($sUnescaped){
  return mysql_real_escape_string($sUnescaped);
}

/** 
 * Returns a row from a recordset as a numbered array
 *
 * @param  recordset $rs 
 * @return array     the row
 * @access public
 */
function db_fetch_row($rs){
  return mysql_fetch_row($rs);
}


/** 
 * Returns a row as an array
 * 
 * @param recordset $rs    The record set from which to retrieve a record
 * @param integer   $iMode (optional) The result type, indicating which type of
 * array will be returned.Options are: MYSQL_ASSOC, MYSQL_BOTH and MYSQL_BOTH.
 * Default is MYSQL_ASSOC.
 */
function db_fetch_array($rs, $mode=NULL) {
   if ($mode == NULL) {
     $mode = MYSQL_ASSOC;
   }

  return mysql_fetch_array($rs, $mode);
}

/** 
 * Returns the name of a field in a recordset
 *
 * @param recordset $rs    A recordset containing the query result
 * @param integer   $index The index of the required field
 * @return string          The name of the field
 * @access public
 */
function db_field_name($rs, $index){ 
  return mysql_field_name($rs, $index);
}

/** 
 * Returns the type of a field in a recordset
 *
 * @param recordset $rs    Aa recordset containing the query result
 * @param integer   $index The index of the required field
 * @return string          The type of the field
 * @access public
 */
function db_field_type($rs, $index){ 
  return mysql_field_type($rs, $index);
}

/** 
 * Frees a resultset
 *
 * @param  resultset $rs
 * @return boolean
 */
function db_free_result($rs){
  return mysql_free_result($rs);
}


/** 
 * Returns  the id of the last inserted record
 *
 * @return int   containing the id
 */
function db_get_generated_key(){ 
  return mysql_insert_id();
}


/** 
 * Queries the database to see if the given table name exists
 * 
 * @param  string    $table The query to be issued to the database
 * @return array            Table status information, or <code>false</code>
 */
function db_is_table($table) {

  global $SPL_CFG;

  $query = "SHOW TABLE STATUS FROM ".$SPL_CFG['dbname']." LIKE '".$SPL_CFG['prefix'].$table."'";
  $result = db_retrieve(db_query($query));
  return $result;

}

/** 
 * Returns the number of fields in a recordset
 *
 * @param  recordset $rs
 * @return integer   the number of fields
 * @access public
 */
function db_num_fields($rs){ 
  return mysql_num_fields($rs);
}

/** 
 * Returns the number of rows in a recordset
 *
 * @param  recordset $rs
 * @return integer       the number of rows
 * @access public
 */
function db_num_rows($rs){ 
  return mysql_num_rows($rs);
}

/** 
 * Queries the database
 * 
 * @param  string    $query The query to be issued to the database
 * @return recordset        A record set, or  <code>false</code>
 */
function db_query($query) {

  global $SPL_CFG,$dbhandle;

  $result = @mysql_query($query, $dbhandle) or die((isset ($SPL_TXT['db_query_error']) ? $SPL_TXT['db_query_error'] : "Database query error")." (".mysql_errno().") ".mysql_error());
  return $result;

}

/** 
 * Retrieves a single record from a recordset
 * 
 * @param recordset $rs    The record set from which to retrieve a record
 * @param integer   $iMode (optional) The result type, indicating which type of
 * array will be returned.Options are: MYSQL_ASSOC, MYSQL_BOTH and MYSQL_BOTH.
 * Default is MYSQL_ASSOC.
 */
 function db_retrieve($rs, $mode=NULL) {
 
   if ($mode == NULL) {
     $mode = MYSQL_ASSOC;
   }

   if (gettype($rs) == "resource") {
     if (get_resource_type($rs) == "mysql result") {
        return mysql_fetch_array($rs, $mode);
     }
   }
}

/** 
 * Retrieves server information
 */
 function db_server_info() {
   return mysql_get_server_info();
 }

/** 
 * Retrieves the database version number
 * 
 */
function db_version(){
  
  global $SPL_CFG,$dbhandle;

  // major_version*10000 + minor_version *100 + sub_version
  // $version = @mysql_get_server_info();
    
  $result = mysql_query("SELECT VERSION() AS version", $dbhandle);

  if ($result != false) {
    $row = mysql_fetch_row($result);
    $match = explode('.', $row[0]);
    mysql_free_result($result);
    if (!isset($row)) {
      return 32332;
    } else {
      return (int)sprintf('%d%02d%02d', $match[0], $match[1], intval($match[2]));
    }
  }
}

/**
 * $Log: db.inc.php,v $
 * Revision 1.10  2006/08/21 19:41:50  jebbol
 * Changed the database connection from $SPL_CFG['dbhandle'] to $dbhandle
 *
 * Revision 1.9  2006/08/08 06:10:08  jebbol
 * Setting handle to null on close
 *
 */

?>
Return current item: Study planner