File: 1.19.1b/server/base/nats.db.inc.php (View as Code)

1: 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: global $NATS; 26: return $NATS->DB->SafeString($s); 27: } 28: 29: class TNATS_DB 30: { 31: var $connected=false; 32: var $sql=0; 33: 34: var $LastError=0; 35: var $LastErrorString=""; 36: 37: function Connect() 38: { 39: global $fnCfg; 40: $this->sql=mysqli_connect($fnCfg['db.server'],$fnCfg['db.username'],$fnCfg['db.password']) 41: or die("Cannot connect to MySQL server"); 42: mysqli_select_db($this->sql,$fnCfg['db.database']) 43: or die("Cannot select MySQL database"); 44: $this->connected=true; 45: return $this->sql; 46: } 47: 48: function Disconnect() 49: { 50: mysqli_close($this->sql); 51: $this->sql=0; 52: $this->connected=false; 53: } 54: 55: function SafeString($s) 56: { 57: return mysqli_real_escape_string($this->sql,$s); 58: } 59: 60: function Query($query,$debugerror=true) 61: { 62: global $NATS; 63: if (!$this->connected) return -1; 64: $result=mysqli_query($this->sql,$query); 65: if ($debugerror) 66: { 67: // persist the last error state 68: $this->LastError=mysqli_errno($this->sql); 69: if ($this->LastError>0) 70: { 71: $this->ErrorString=mysqli_error($this->sql)." (".mysqli_errno($this->sql).")"; 72: } 73: else $this->ErrorString=""; 74: } 75: 76: if (mysqli_errno($this->sql)>0) 77: { 78: $err=mysqli_error($this->sql)." (".mysqli_errno($this->sql).")"; 79: if (isset($NATS)&&$debugerror) 80: { 81: $NATS->Event("Query Failed: ".$query,2,"DB","Query"); 82: $NATS->Event("Query Error: ".$err,2,"DB","Query"); 83: } 84: } 85: return $result; 86: } 87: 88: function Free(&$result) 89: { 90: mysqli_free_result($result); 91: } 92: 93: function Fetch_Array(&$result) 94: { 95: return mysqli_fetch_array($result); 96: } 97: 98: function Affected_Rows() 99: { 100: return mysqli_affected_rows($this->sql); 101: } 102: 103: function Insert_Id() 104: { 105: return mysqli_insert_id($this->sql); 106: } 107: 108: function Num_Rows(&$result) 109: { 110: return mysqli_num_rows($result); 111: } 112: 113: function Error() 114: { 115: if ($this->LastError==0) return false; 116: return true; 117: } 118: 119: function Error_Number() 120: { 121: return $this->LastError; 122: } 123: 124: function Error_String() 125: { 126: return $this->ErrorString; 127: } 128: }