| [ PHPXref.com ] | [ Generated: Sun Jul 20 18:31:52 2008 ] | [ Mailing List 1.03 ] |
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
1 <?php 2 define("DB_RT_ARRAY",0); 3 define("DB_RT_OBJECT",1); 4 define("DB_RT_SGLINE",2); 5 class CDatabase{ 6 var $type; 7 var $conn_id; 8 var $current_db; 9 var $num_queries; 10 var $modif = FALSE; 11 function CDatabase($connect_params = "") { 12 $this->name = "database"; 13 $this->type = $type; 14 if ($connect_params != "") 15 $this->Connect($connect_params); 16 } 17 function Connect($connect_params = "") { 18 extract($connect_params); 19 $this->conn_id = mysql_connect($server,$login,$password,TRUE) or die("CDatabase::Connect() error " . mysql_error($this->conn_id)); 20 if ($default != "") 21 $this->SelectDB($default); 22 } 23 function Close() { 24 mysql_close($this->conn_id); 25 } 26 function SelectDB($database) { 27 mysql_select_db($database,$this->conn_id) or die("CDatabase::SelectDB() error"); 28 $this->current_db = $database; 29 } 30 function Query($query,$db = "") { 31 $this->num_queries++; 32 if ($db) 33 $result = mysql_db_query($db ,$query,$this->conn_id); 34 else 35 $result = mysql_query($query,$this->conn_id) or die($query . mysql_error()); 36 if (in_array(substr($query,0,strpos($query," ")),array("INSERT", "UPDATE", "DELETE"))) 37 $this->modif = TRUE; 38 return $result; 39 } 40 function FetchObject($result) { 41 return mysql_fetch_object($result); 42 } 43 function FetchRow($result) { 44 $data = mysql_fetch_row($result); 45 if (is_array($data)) { 46 foreach ($data as $key => $val) 47 $data[$key] = stripslashes($val); 48 } 49 return $data; 50 } 51 function FetchArray($result,$result_type = MYSQL_ASSOC) { 52 $data = mysql_fetch_array($result,$result_type); 53 if (is_array($data)) { 54 foreach ($data as $key => $val) 55 $data[$key] = stripslashes($val); 56 } 57 return $data; 58 } 59 function NumRows($result) { 60 return mysql_num_rows($result); 61 } 62 function AffectedRows() { 63 return mysql_affected_rows($this->conn_id); 64 } 65 function InsertID() { 66 return mysql_insert_id($this->conn_id); 67 } 68 function NumQueries() { 69 return $this->num_queries; 70 } 71 function QFetchObject($query) { 72 return $this->FetchObject($this->Query($query)); 73 } 74 function QFetchRow($query) { 75 $data = $this->FetchRow($this->Query($query)); 76 if (is_array($data)) { 77 foreach ($data as $key => $val) 78 $data[$key] = stripslashes($val); 79 } 80 return $data; 81 } 82 function QFetchArray($query) { 83 $data = $this->FetchArray($this->Query($query)); 84 if (is_array($data)) { 85 foreach ($data as $key => $val) 86 $data[$key] = stripslashes($val); 87 } 88 return $data; 89 } 90 function RowCount($table,$where_clause = "",$what="") { 91 $what = $what != "" ? $what : "*" ; 92 $result = $this->FetchRow($this->Query("SELECT COUNT({$what}) FROM $table $where_clause;")); 93 return $result[0]; 94 } 95 function FetchRowArray($result,$return_type = DB_RT_ARRAY,$key = "") { 96 $ret_val = array(); 97 $i = 0; 98 while ($row = (($return_type == DB_RT_ARRAY) ? $this->FetchArray($result) : $this->FetchObject($result))) 99 $ret_val[(($key == "") ? $i++ : (($return_type == DB_RT_ARRAY) ? $row["$key"] : $row->$key))] = $row; 100 return (count($ret_val) != 0) ? $ret_val : NULL; 101 } 102 function QFetchRowArray($query,$return_type = DB_RT_ARRAY,$key = "") { 103 if ($return_type == DB_RT_SGLINE) { 104 $return_type = 0; 105 $change = 1; 106 } 107 $data = $this->FetchRowArray($this->Query($query),$return_type,$key); 108 if (($change == 1) && is_array($data)) { 109 foreach ($data as $k => $val) { 110 $data[$k] = $val[key($val)]; 111 } 112 } 113 return $data; 114 } 115 function GetTableFields($table) { 116 $fields = $this->QFetchRowArray("SHOW FIELDS FROM `$table`"); 117 $ret_val = array(); 118 foreach ($fields as $field) 119 $ret_val[] = $field["Field"]; 120 return $ret_val; 121 } 122 function QuerySelectByID($table,$id,$fields = "*",$return_type = DB_RT_ARRAY) { 123 $query = "SELECT $fields FROM `$table` WHERE `id` = '$id'"; 124 return ($return_type == DB_RT_ARRAY) ? $this->QFetchArray($query) : $this->QFetchObject($query); 125 } 126 function QuerySelectLimit($table,$fields,$where_clause,$start,$count,$pm = TRUE,$order_by = "",$order_dir = "ASC",$return_type = DB_RT_ARRAY) { 127 $count = ($count == "") ? 0 : $count; 128 $_start = ($pm == TRUE) ? ((($start == 0) ? 1 : $start) * $count - $count) : $start; 129 $order_clause = ($order_by != "") ? "ORDER BY $order_by " . (in_array($order_dir,array("ASC","DESC")) ? "$order_dir " : "") : ""; 130 $where_clause = ($where_clause != "") ? "WHERE $where_clause " : ""; 131 $limit_clause = ($start >= 0) ? "LIMIT $_start,$count" : ""; 132 $query = "SELECT $fields FROM `$table` {$where_clause}{$order_clause}{$limit_clause}"; 133 return $this->QFetchRowArray($query,$return_type); 134 } 135 function QueryInsert($table,$fields) { 136 $table_fields = $this->GetTableFields($table); 137 if (count($fields) == 0) { 138 $names[] = "id"; 139 $values[] = "''"; 140 } else 141 foreach ($fields as $field => $value) 142 if (in_array($field,$table_fields)) { 143 $names[] = "`$field`"; 144 $values[] = is_numeric($value) ? $value : "'" . (stripslashes($value) == addslashes($value) ? addslashes($value) : $value) . "'"; 145 } 146 $names = implode(",",$names); 147 $values = implode(",",$values); 148 $this->Query("INSERT INTO `$table` ($names) VALUES($values)"); 149 return $this->InsertID(); 150 } 151 function QueryUpdate($table,$fields,$where_clause) { 152 if (is_array($fields)) { 153 $table_fields = $this->GetTableFields($table); 154 foreach ($fields as $field => $value) 155 if (in_array($field,$table_fields)) 156 $pairs[] = "`$field` = " . (is_numeric($value) ? $value : "'" . (stripslashes($value) == addslashes($value) ? addslashes($value) : $value) . "'"); 157 if (is_array($pairs)) 158 $this->Query("UPDATE `$table` SET " . implode(", ",$pairs) . " WHERE($where_clause)"); 159 } 160 } 161 function QueryUpdateByID($table,$fields) { 162 $id = $fields["id"]; 163 unset($fields["id"]); 164 $this->QueryUpdate($table,$fields,"`id` = '$id'"); 165 } 166 } 167 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| [ Powered by PHPXref - Served by Debian GNU/Linux ] |