Location: PHPKode > scripts > Prioritexter > Db.php
<?php

// brought into line with phpplatform on 12/28/04
class Db {

  var $host, $db, $user, $pass, $link, $db_prefix;

  function Db() {
    // set this constant value
    $this->db_prefix = DB_PROJECT_NICKNAME . "_". DB_PROJECT_NAME ."_";
    $this->host = "localhost";
    $this->db = DB_DBNAME;
    $this->user = DB_USER;
    $this->pass = DB_PASS;
    $this->link = mysql_connect($this->host, $this->user, $this->pass);
    mysql_select_db($this->db) 
      or Error::dbErr("Could not select database.");
    register_shutdown_function(array( &$this, "close" ));
  }    

  function close() {
    mysql_close($this->link);
  }

  function testTable($table_name) {
    $query_str = "SELECT * FROM " . $this->db_prefix . $table_name;
    if(mysql_query( $query_str, $this->link )===FALSE) {
      return false;
    } else {
      return true;
    }
  }

  function query( $query_str ) {
    Debug::debug("<p>$query_str\r");
    $return = mysql_query( $query_str, $this->link ) OR 
       Error::dbErr( $query_str );
    return $return;
  }

  function insert($table_name, $column_values=array()) {
    Assert::assert(is_array($column_values),"not an array");
    $column_values = Text::cleanArray($column_values);
    $query_str = "INSERT INTO " . $this->db_prefix . $table_name ." (";
    foreach($column_values as $col=>$val)
      $query_str .=  $col .", ";
    $query_str = Text::removeLastChars($query_str,2);
    $query_str .= ") VALUES (";
    foreach($column_values as $val)
      $query_str .= "'". $val ."', ";
    $query_str = Text::removeLastChars($query_str,2);
    $query_str .= ")";    
    return $this->query( $query_str );
  }

  function update($table_name, $column_values=array(), $where_str) {
    Assert::assert(is_array($column_values),"not an array");
    $column_values = Text::cleanArray($column_values);
    $query_str = "UPDATE " . $this->db_prefix . $table_name ." SET ";
    foreach($column_values as $col=>$val)
      $query_str .= "$col = '". $val ."', ";
    $query_str = Text::removeLastChars($query_str,2);
    if ($where_str)
      $query_str .= " WHERE " . $where_str;
    return $this->query( $query_str );
  }

  function delete($table_name, $where_str) {
    if (strlen($where_str)<3) Error::error("Incorrect where string in delete");
    $query_str = "DELETE FROM " . $this->db_prefix . $table_name;
    $query_str .= " WHERE " . $where_str;
    return $this->query( $query_str );
  }

  function joinQuery($type_1, $type_2, $join_field, 
		     $where_str="", $order_str="") {
    $query_str = "SELECT * FROM " . $this->db_prefix . $type_1
       . "," . $this->db_prefix . $type_2 . " WHERE "
       . $this->db_prefix . $type_1 . "." . $join_field . "="
       . $this->db_prefix . $type_2 . "." . $join_field;
    if ($where_str)
      $query_str .= " AND " . $where_str;
    if ($order_str) 
      $query_str .= " ORDER BY " . $order_str;
    return $this->query( $query_str );
  }
  
  function simpleSelect($table_type, $where_str="", $order_str="") {
    $query_str = "SELECT * FROM " . $this->db_prefix . $table_type;
    if ($where_str)
      $query_str .= " WHERE " . $where_str;
    if ($order_str) 
      $query_str .= " ORDER BY " . $order_str;
    return $this->query( $query_str );
  }

  function getOneRecord($table_type, $where_str="", $order_str="") {
    $result = $this->simpleSelect($table_type, $where_str, $order_str);
    $myrow = mysql_fetch_array($result);
    if (sizeof($myrow) == 0) return false;
    else return $myrow;
  }

  // thanks to chrisshaffer at bellsouth dot net, copyright 2002
  function getEnumValues($table_name,$field_name) {
    $result = mysql_query("DESCRIBE ".$this->db_prefix . $table_name);
    
    // go through all the fields, which are returned by row:
    while($row = mysql_fetch_array($result)) {      
      // row is mysql type, in format "int(11) unsigned zerofill"
      // or "enum('cheese','salmon')" etc.

      // BUT... is it the RIGHT field? is it the one we were told to find?
      if ($row['Field']==$field_name) {
	ereg('^([^ (]+)(\((.+)\))?([ ](.+))?$',$row['Type'],$fieldTypeSplit);
	// split type up into array
	$fieldType = $fieldTypeSplit[1];// eg 'int' for integer.
	$fieldLen = $fieldTypeSplit[3]; // eg 'cheese','salmon' for enum.      

	if (($fieldType=='enum' || $fieldType=='set'))	{
	  $fieldOptions = split("','",substr($fieldLen,1,-1));
	  return $fieldOptions;
	}
      }
    }
    //if the funciton makes it this far, then it either
    //did not find an enum/set field type, or it
    //failed to find the the fieldname, so exit FALSE!
    return FALSE;
  }

  function lastInsertAutoIncId() {
    Debug::debug("last id inserted was ".  mysql_insert_id($this->link));
    return mysql_insert_id($this->link);
  }
}
?>
Return current item: Prioritexter