Changeset 485 for trunk/phpbms/include/db.php
- Timestamp:
- 04/07/09 11:44:18 (3 years ago)
- Files:
-
- 1 modified
-
trunk/phpbms/include/db.php (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/phpbms/include/db.php
r427 r485 1 <?php 1 <?php 2 2 /* 3 3 $Rev: 249 $ | $LastChangedBy: brieb $ … … 41 41 //we may want to do more than connect via mysql; 42 42 var $type="mysql"; 43 43 44 44 // mysql vars; 45 45 var $db_link; … … 49 49 var $dbpass; 50 50 var $pconnect=true; 51 51 52 52 var $showError=false; 53 53 var $logError=true; 54 54 var $stopOnError=true; 55 55 var $errorFormat="xhtml"; 56 56 57 57 var $error = NULL; 58 58 59 59 function db($connect = true, $hostname = NULL, $schema = NULL, $user = NULL, $pass = NULL, $pconnect = NULL, $type = "mysql"){ 60 60 61 61 if($type!="mysql") 62 62 $this->type=$type; … … 69 69 if($hostname!=NULL) 70 70 $this->hostname = $hostname; 71 71 72 72 if(defined("MYSQL_DATABASE")) 73 73 $this->schema = MYSQL_DATABASE; 74 74 if($schema!=NULL) 75 75 $this->schema = $schema; 76 76 77 77 if(defined("MYSQL_USER")) 78 78 $this->dbuser = MYSQL_USER; 79 79 if($schema!=NULL) 80 80 $this->dbuser = $user; 81 81 82 82 if(defined("MYSQL_USERPASS")) 83 83 $this->dbpass = MYSQL_USERPASS; 84 84 if($schema!=NULL) 85 85 $this->dbpass = $pass; 86 86 87 87 if(defined("MYSQL_PCONNECT")) 88 88 $this->pconnect = MYSQL_PCONNECT; 89 89 if($pconnect!=NULL) 90 90 $this->pconnect = $pconnect; 91 break; 91 break; 92 92 } 93 93 94 94 if($connect){ 95 95 if($this->connect()){ … … 97 97 return $this->db_link; 98 98 else 99 return false; 99 return false; 100 100 } else 101 101 return false; … … 106 106 function connect(){ 107 107 // This functions connects to the database. It uses pconnect if the variable is set; 108 108 109 109 if($this->pconnect) 110 110 $this->db_link = @ mysql_pconnect($this->hostname,$this->dbuser,$this->dbpass); 111 111 else 112 112 $this->db_link = @ mysql_connect($this->hostname,$this->dbuser,$this->dbpass); 113 113 114 if(!$this->db_link){ 115 114 116 $error = new appError(-400,"Could not connect to database server.\n\n".$this->getError(),"",$this->showError,$this->stopOnError,false,$this->errorFormat); 115 117 return false; 116 } else 118 119 } else 117 120 return $this->db_link; 118 } 119 120 121 122 }//end function connect 123 124 121 125 function selectSchema($schema=NULL){ 122 126 if($schema!=NULL) 123 127 $this->schema=$schema; 124 128 125 129 if(! @ mysql_select_db($this->schema,$this->db_link)){ 126 $error = new appError(-410,"Could not open schema ".$this->schema,"",$this->showError,$this->stopOnError,false,$this->errorFormat); 130 $error = new appError(-410,"Could not open schema ".$this->schema,"",$this->showError,$this->stopOnError,false,$this->errorFormat); 127 131 return false; 128 132 } else 129 return true; 133 return true; 130 134 } 131 135 … … 134 138 switch($this->type){ 135 139 case "mysql": 136 if(!isset($this->db_link)) 137 if(!$this->d ataB()) die($this->error);140 if(!isset($this->db_link)) 141 if(!$this->db()) die($this->error); 138 142 $queryresult = @ mysql_query($sqlstatement,$this->db_link); 139 143 if(!$queryresult){ … … 141 145 $error = new appError(-420,$this->getError($this->db_link)."\n\nStatement: ".$sqlstatement,"",$this->showError,$this->stopOnError,$this->logError,$this->errorFormat); 142 146 return false; 143 } 144 break; 145 }//end case 146 147 } 148 break; 149 }//end case 150 147 151 $this->error=NULL; 148 152 return $queryresult; … … 153 157 154 158 switch($this->type){ 155 case "mysql": 159 case "mysql": 156 160 @ mysql_query("SET NAMES ".$encoding, $this->db_link); 157 161 break; 158 162 159 163 }//endswitch 160 164 … … 163 167 164 168 function getError($link = NULL){ 165 166 switch($this->type){ 167 case "mysql": 168 $thereturn = mysql_error($link); 169 170 switch($this->type){ 171 case "mysql": 172 if($link) 173 $thereturn = @ mysql_error($link); 174 else 175 $thereturn = @ mysql_error(); 169 176 break; 170 177 }//end switch --type-- 171 172 return $thereturn; 173 178 179 return $thereturn; 180 174 181 }//end method --getError-- 175 182 176 183 177 184 function numRows($queryresult){ … … 200 207 }//end function 201 208 202 209 203 210 function startTransaction(){ 204 205 switch($this->type){ 206 211 212 switch($this->type){ 213 207 214 case "mysql": 208 215 $this->query("START TRANSACTION;"); 209 216 break; 210 217 211 218 }//end switch 212 219 213 220 }//end method --startTransaction-- 214 215 221 222 216 223 function commitTransaction(){ 217 218 switch($this->type){ 219 224 225 switch($this->type){ 226 220 227 case "mysql": 221 228 $this->query("COMMIT;"); 222 229 break; 223 230 224 231 }//end switch 225 232 226 233 }//end method --startTransaction-- 227 228 234 235 229 236 function rollbackTransaction(){ 230 231 switch($this->type){ 232 237 238 switch($this->type){ 239 233 240 case "mysql": 234 241 $this->query("ROLLBACK;"); 235 242 break; 236 243 237 244 }//end switch 238 245 239 246 }//end method --startTransaction-- 240 247 241 248 242 249 function seek($queryresult,$rownum){ … … 278 285 return $thereturn; 279 286 }//end function 280 281 287 288 282 289 function tableInfo($tablename){ 283 290 //this function returns a multi-dimensional array describing the fields in a given table … … 291 298 $thereturn[$name]["type"] = @ mysql_field_type($queryresult,$offset); 292 299 $thereturn[$name]["length"] = mysql_field_len($queryresult,$offset); 293 $thereturn[$name]["flags"] = mysql_field_flags($queryresult,$offset); 300 $thereturn[$name]["flags"] = mysql_field_flags($queryresult,$offset); 294 301 } 295 302 } … … 305 312 case "mysql": 306 313 $thereturn = @ mysql_insert_id($this->db_link); 307 break; 314 break; 308 315 } 309 316 310 317 return $thereturn; 311 318 } 312 319 313 320 function affectedRows(){ 314 321 $thereturn = false; … … 316 323 case "mysql": 317 324 $thereturn = @ mysql_affected_rows($this->db_link); 318 break; 325 break; 319 326 } 320 327 321 328 return $thereturn; 322 329 } 323 330 324 331 }//end db class 325 332 ?>