File: 1.11.2a/server/base/nats.db.inc.php (View as HTML)

  1: <?php // nats.db.inc.php -- nats db module class
  2: /* -------------------------------------------------------------
  3: This file is part of FreeNATS
  4: 
  5: FreeNATS is (C) Copyright 2008 PurplePixie Systems
  6: 
  7: FreeNATS is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeNATS is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with Foobar.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freenats
 21: -------------------------------------------------------------- */
 22: 
 23: function ss($s) // safestring
 24: {
 25: return mysql_escape_string($s);
 26: }
 27: 
 28: class TNATS_DB
 29: 	{
 30: 	var $connected=false;
 31: 	var $sql=0;
 32: 	
 33: 	var $LastError=0;
 34: 	var $LastErrorString="";
 35: 	
 36: 	function Connect()
 37: 		{
 38: 		global $fnCfg;
 39: 		$this->sql=mysql_connect($fnCfg['db.server'],$fnCfg['db.username'],$fnCfg['db.password'])
 40: 			or die("Cannot connect to MySQL server");
 41: 		mysql_select_db($fnCfg['db.database'])
 42: 			or die("Cannot select MySQL database");
 43: 		$this->connected=true;
 44: 		return $this->sql;
 45: 		}
 46: 		
 47: 	function Disconnect()
 48: 		{
 49: 		mysql_close($this->sql);
 50: 		$this->sql=0;
 51: 		$this->connected=false;
 52: 		}
 53: 		
 54: 	function Query($query,$debugerror=true)
 55: 		{
 56: 		global $NATS;
 57: 		if (!$this->connected) return -1;
 58: 		$result=mysql_query($query,$this->sql);
 59: 		if ($debugerror)
 60: 			{
 61: 			// persist the last error state
 62: 			$this->LastError=mysql_errno($this->sql);
 63: 			if ($this->LastError>0)
 64: 				{
 65: 				$this->ErrorString=mysql_error($this->sql)." (".mysql_errno($this->sql).")";
 66: 				}
 67: 			else $this->ErrorString="";
 68: 			}
 69: 			
 70: 		if (mysql_errno($this->sql)>0)
 71: 			{
 72: 			$err=mysql_error($this->sql)." (".mysql_errno($this->sql).")";
 73: 			if (isset($NATS)&&$debugerror)
 74: 				{
 75: 				$NATS->Event("Query Failed: ".$query,2,"DB","Query");
 76: 				$NATS->Event("Query Error: ".$err,2,"DB","Query");
 77: 				}
 78: 			}
 79: 		return $result;
 80: 		}
 81: 		
 82: 	function Free(&$result)
 83: 		{
 84: 		mysql_free_result($result);
 85: 		}
 86: 		
 87: 	function Fetch_Array(&$result)
 88: 		{
 89: 		return mysql_fetch_array($result);
 90: 		}
 91: 		
 92: 	function Affected_Rows()
 93: 		{
 94: 		return mysql_affected_rows($this->sql);
 95: 		}
 96: 		
 97: 	function Insert_Id()
 98: 		{
 99: 		return mysql_insert_id($this->sql);
100: 		}
101: 		
102: 	function Num_Rows(&$result)
103: 		{
104: 		return mysql_num_rows($result);
105: 		}
106: 		
107: 	function Error()
108: 		{
109: 		//if (mysql_errno($this->sql)==0) return false;
110: 		//return true;
111: 		if ($this->LastError==0) return false;
112: 		return true;
113: 		}
114: 		
115: 	function Error_Number()
116: 		{
117: 		//return mysql_errno($this->sql);
118: 		return $this->LastError;
119: 		}
120: 		
121: 	function Error_String()
122: 		{
123: 		return $this->ErrorString;
124: 		//return mysql_error($this->sql)." (".$this->Error_Number().")";
125: 		}
126: 	}