link = $db_link; $this->user = $db_user; $this->pass = $db_pass; $this->name = $db_name; $return = $this->open(); $charset_result = "SET character_set_results = $db_charset"; $charset_client = "SET character_set_client = $db_charset"; $charset_connection = "SET character_set_connection = $db_charset"; // $charset_names = "SET NAME '$db_charset'"; mysql_query($charset_result); mysql_query($charset_client); mysql_query($charset_connection); // mysql_query($charset_names); return $return; } function open(){ $conn = mysql_connect($this->link, $this->user, $this->pass); $return = false; if (!$conn){ echo zprava(CHYBA_DATABAZE); //return false; //exit; } else { mysql_select_db($this->name); $return = true; } return $return; } function info() { echo "LINK: $this->link, USER: $this->user, PASS: $this->user, NAME: $this->name"; } function query($query){ $result = mysql_db_query($this->name, $query); if (!$result) { $text = CHYBA_VYBERU . ": " . $query; echo $query; echo zprava($text); exit(); } return $result; } function save_in_file($query){ $file = fopen("log_sql.txt", "a"); fwrite($file, $query . "; "); fclose($file); } function fetch_array($res){ $result = mysql_fetch_array($res); return $result; } function num_rows($res){ $result = mysql_num_rows($res); if (!$result): $result=0; endif; return $result; } function free_query($query){ $result = mysql_free_result($query); if (!$result): $text = CHYBA_UVOLNENI_VYBERU; echo zprava($text); exit; endif; } function insert($table, $fields, $values, $func = array()){ $query = "INSERT INTO " . $table . " ("; for ($i=0; $iquery($query)) { if ($this->save_in_file == 1) $this->save_in_file($query); } return $result; } function update($table, $fields, $values, $name_id = "", $id = "", $func = array()){ $query = "UPDATE " . $table . " SET "; for ($i=0; $iquery($query)) { if ($this->save_in_file == 1) $this->save_in_file($query); } return $result; } function delete($table, $name_id, $id){ $query = "DELETE FROM " . $table . " WHERE $name_id=$id"; if ($result = $this->query($query)) { if ($this->save_in_file == 1) $this->save_in_file($query); } } function get_id($table, $fields, $values, $name_id){ $query = "SELECT $name_id FROM " . $table . " WHERE "; for ($i=0; $iquery($query); $record = $this->fetch_array($result); return $record[$name_id]; } function max_id($table, $fields_id, $where = ""){ $query = "SELECT MAX($fields_id) AS max_id FROM $table"; $query .= !empty($where) ? " WHERE $where" : ""; $result = $this->query($query); $record = $this->fetch_array($result); return $record["max_id"]; } function next_record($table, $field_id, $id, $order, $asc){ $ordered = $asc == 0 ? "ASC" : "DESC"; $query = "SELECT $field_id FROM $table ORDER BY $order $ordered, $field_id $ordered"; echo $query . BR; $result = $this->query($query); $rows = $this->num_rows($result); $endfor = false; $prev_id = -1; $next_id = -1; $current_id = 0; for ($i = 0; $i < $rows; $i++){ $prev_id = $endfor ? $prev_id : $current_id; $record = $this->fetch_array($result); $current_id = $record[$field_id]; $next_id = $endfor ? $current_id : $next_id; if ($endfor){ break; } else { $endfor = $current_id == $id ? true : false; } //echo "prev: $prev_id, current: $current_id, next: $next_id" . BR; } $id = $next_id == -1 ? $prev_id : $next_id; //echo "NEXT_ID: $id" . BR; return $id; } function increase_number($table, $field, $field_id, $id){ $number = $this->max_id($table, $field, $field_id . " = " . $id) + 1; $this->update($table, array($field), array($number), $field_id, $id); } function table_number_of_records($table, $join = "", $where = ""){ $query = "SELECT COUNT(*) as number FROM $table"; $query .= !empty($join) ? " " . $join : ""; $query .= !empty($where) ? " WHERE " . $where : ""; //echo $query . BR; $result = $this->query($query); $record = $this->fetch_array($result); return $record["number"]; } function get_value($table, $field, $value, $return_field, $id_field, $id_value){ $return = ""; $query = "SELECT $return_field FROM $table WHERE $field = '$value' AND $id_field = $id_value"; $result = $this->query($query); if ($this->num_rows($result) > 0){ while ($record = $this->fetch_array($result)){ $return = $record[$return_field]; } } return $return; } } ?>