fhiiqm/inc/dbconnect.inc.php
author Bettina Schwarzer <schwarzer@fhi-berlin.mpg.de>
Wed, 31 Aug 2011 14:22:19 +0200
changeset 1 6288d5685bff
child 13 741b35e9ebbe
permissions -rw-r--r--
Stand v. 31.08.2011

<?php
//include_once("configy.inc.php");
/**
 * dbconnection
 * 
 * @package fhiiqm  
 * @author IQM achilleus
 * @copyright Bettina Schwarzer
 * @version 05/2011
 * @access public
 */
class dbconnection
  { protected $_server;
    protected $_user;
    protected $_passwort;
    protected $_dbname;
	var $_dbtyp;

    protected $_connid;
    protected $_statement;
    var $error;
    var $_lastmessage;

    public $result;

	function __construct()
	{
		include_once("conf.inc.php");
//		global $config;

        $this->_server   =  ( isset($server)   && $server   ) ? $server   : $config["server"];
        $this->_user     =  ( isset($user)     && $user     ) ? $user     : $config["user"];
        $this->_passwort =  ( isset($passwort) && $passwort ) ? $passwort : $config["passwort"];
        $this->_dbname   =  ( isset($dbname)   && $dbname   ) ? $dbname   : $config["dbname"];

        $this->_connid = false;
        $this->error         = "";
        $this->_lastmessage   = "";
//		$this -> _connid = @new mysqli($this->_server, $this->_user, $this->_passwort, $this->_dbname);
        $this->_connid = mysqli_init();
        $this->_connid->real_connect($this->_server, $this->_user, $this->_passwort, $this->_dbname);
//        $this->_connid->set_charset("utf8");		
//        echo $this-> _connid->character_set_name()."<br>\n";
//        $this -> _connid -> set_charset('latin1'); //latin1_swedish_ci, ist Standard
//        echo $this-> _connid->character_set_name()."<br>\n";

		if (mysqli_connect_errno())
		{
			$this->error = mysqli_connect_error();
        	$this -> _connid = false;
//			printf("keine Verbindung zu  %s, Fehler: %s", $config["dbname"], $this->error); 
			exit();
		} 
//		printf("Verbindung zu  %s steht<br>\n", $config["dbname"]); 
//		echo "Verbindung zu " . $config["dbname"] . " steht.<br>\n"; 
	}

	function __destruct()
	{
		$this -> close();
	}

	function close()
	{
		if ($this -> _connid)
		{
			$this -> _connid -> close();
        		$this -> _connid = false;
		}
		$this->error = "";

	}

	function queryObjectArray($sql)
	{// in $result_arr steht Array des Ergebnisses als Objekt
		if ( is_string($sql) )
		{
			if ($this->result = $this->_connid->query($sql))
			{
				if ($this->result->num_rows)
				{
					while ($row = $this->result->fetch_object())
						$result_arr[] = $row;
					$this->result->free();
                    $this->_connid->next_result(); // wegen OUT-Parameter in stored procedures
                    return $result_arr;
				}
				else
				{
					$this->error = "";
					return false;
				}
			}
			else
			{
				$this->error = $this->_connid->error;
				return false;
			}
		}
		else
		{
			$this->error = "'$sql'<br> ist kein SQL-Statement!";
			return false;
		}
	
	}

	function querySingleItem($sql)
	{// gibt nur 1 Zeile mit einer Spalte zurueck, z.B. Count()
		if ($this->result = $this->_connid->query($sql))
		{
			if ($row = $this->result->fetch_array())
			{
				$this->result->close();
				return $row[0];
			}
			else return false;
		}
		else
		{
			$this->error = $this->_connid->error;
			return false;

		}
	}

	function execute($sql)
	{// INSERT, UPDATE, DELETE
		if ($this->_connid->real_query($sql))
			return true;
		else
        {
            $this->error = $this->_connid->errno . ": " . $this->_connid->error;
 			return false;
       }
	}

	function insertId()
	{// liefert letzten Autowert bei INSERT
		return $this->_connid->insert_id;
	}
	
	function numrows($sql)
	{// Anzahl der Ergebniszeilen
		if ($this->result = $this->_connid->query($sql))
		{
			return $this->result -> num_rows;
		}
		else
			return 0;
	}
	
	function setcharset($string)
	{// Zeichensatz fuer Client-Verbindung festlegen
		if ($this -> _connid) $this ->_connid -> set_charset($string);
	}
    
    function escapestring($string)
   	{// string escapen: '," ersetzen durch \',\"
		if ($this -> _connid) return $this ->_connid -> real_escape_string($string);
	}

    function getclient_info()
   	{// MySQL-Information
		if ($this -> _connid) return $this ->_connid -> get_client_info();
	}

    function gethost_info()
   	{// Typ der Verbindung zur DB
		if ($this -> _connid) return $this ->_connid -> get_server_info();
	}

    function stmtinit()
    {// $_statement wird als Objekt der mysqli-Klasse erstellt, damit simd alle Methoden der MySQLi_STMT class verfuegbar
       if ($this -> _connid) return $this->_statement = $this->_connid->stmt_init();
    }   
}

/**
 * DateTime_s
 * 
 * @package fhiiqm  
 * @author IQM achilleus
 * @copyright Bettina Schwarzer
 * @version 07/2011
 * @access public
 */
class DateTime_s extends DateTime
{
        public function __toString() 
        {   // Datetime -> String fuer INSERT/UPDATE
            return $this->format('Y-m-d H:i:s');
        }
}
?>