Перейти к содержанию
СофтФорум - всё о компьютерах и не только

Оптимизация PHP кода


Рекомендуемые сообщения

http://pastebin.mozilla-russia.org/102573

Пишу скрипт вывода статистики игроков онлайн игры. Прошу помощи. По второй ссылке уже кое-что сделал сам :thumbup:

Ссылка на комментарий
Поделиться на другие сайты

  • 2 недели спустя...

get_level не можно ни прочитать, ни чтото сделать с ней... что скажете об такой функцие:

function get_level($EXP) {if (!$EXP) {return false;}$levels = array(650, 1917, 4230,  8693, 14583, 22885...);for ($i = 0; $i<count($levels); $i++) {  if ($EXP < $levels[$i])  return $i+1; // +1 чтоб начинать с 1 а не с 0}}
Ссылка на комментарий
Поделиться на другие сайты

  • 3 года спустя...

Помогите оптимизировать данный код .

function unreg($username) { $q = "DELETE from " . TB_PREFIX . "activate where username = '$username'"; return mysql_query($q, $this->connection);}function deleteReinf($id) { $q = "DELETE from " . TB_PREFIX . "enforcement where id = '$id'"; mysql_query($q, $this->connection);}function updateResource($vid, $what, $number) { $q = "UPDATE " . TB_PREFIX . "vdata set " . $what . "=" . $number . " where wref = $vid"; $result = mysql_query($q, $this->connection); return mysql_query($q, $this->connection);}function checkExist($ref, $mode) { if(!$mode) {  $q = "SELECT username FROM " . TB_PREFIX . "users where username = '$ref' LIMIT 1"; } else {  $q = "SELECT email FROM " . TB_PREFIX . "users where email = '$ref' LIMIT 1"; } $result = mysql_query($q, $this->connection); if(mysql_num_rows($result)) {  return true; } else {  return false; }}function checkExist_activate($ref, $mode) { if(!$mode) {  $q = "SELECT username FROM " . TB_PREFIX . "activate where username = '$ref' LIMIT 1"; } else {  $q = "SELECT email FROM " . TB_PREFIX . "activate where email = '$ref' LIMIT 1"; } $result = mysql_query($q, $this->connection); if(mysql_num_rows($result)) {  return true; } else {  return false; }}public function hasBeginnerProtection($vid) { $q = "SELECT u.protect FROM ".TB_PREFIX."users u,".TB_PREFIX."vdata v WHERE u.id=v.owner AND v.wref=".$vid; $result = mysql_query($q, $this->connection); $dbarray = mysql_fetch_array($result); if(!empty($dbarray)) {  if(time()<$dbarray[0]) {   return true;  } else {   return false;  } } else {  return false; }}function updateUserField($ref, $field, $value, $switch) { if(!$switch) {  $q = "UPDATE " . TB_PREFIX . "users set $field = '$value' where username = '$ref'"; } else {  $q = "UPDATE " . TB_PREFIX . "users set $field = '$value' where id = '$ref'"; } return mysql_query($q, $this->connection);}function getSitee($uid) { $q = "SELECT id from " . TB_PREFIX . "users where sit1 = $uid or sit2 = $uid"; $result = mysql_query($q, $this->connection); return $this->mysql_fetch_all($result);}function getVilWref($x, $y) { $q = "SELECT * FROM " . TB_PREFIX . "wdata where x = $x AND y = $y"; $result = mysql_query($q, $this->connection); $dbarray = mysql_fetch_array($result); return $dbarray['id'];}function removeMeSit($uid, $uid2) { $q = "UPDATE " . TB_PREFIX . "users set sit1 = 0 where id = $uid and sit1 = $uid2"; mysql_query($q, $this->connection); $q2 = "UPDATE " . TB_PREFIX . "users set sit2 = 0 where id = $uid and sit2 = $uid2"; mysql_query($q2, $this->connection);}function getUserField($ref, $field, $mode) { if(!$mode) {  $q = "SELECT $field FROM " . TB_PREFIX . "users where id = '$ref'"; } else {  $q = "SELECT $field FROM " . TB_PREFIX . "users where username = '$ref'"; } $result = mysql_query($q, $this->connection) or die(mysql_error()); $dbarray = mysql_fetch_array($result); return $dbarray[$field];}function getInvitedUser($uid) { $q = "SELECT * FROM " . TB_PREFIX . "users where invited = $uid order by regtime desc"; $result = mysql_query($q, $this->connection); return $this->mysql_fetch_all($result);}function getVrefField($ref, $field){  $q = "SELECT $field FROM " . TB_PREFIX . "vdata where wref = '$ref'";  $result = mysql_query($q, $this->connection) or die(mysql_error());  $dbarray = mysql_fetch_array($result);  return $dbarray[$field];}function getVrefCapital($ref){ $q = "SELECT * FROM " . TB_PREFIX . "vdata where owner = '$ref' and capital = 1"; $result = mysql_query($q, $this->connection) or die(mysql_error()); $dbarray = mysql_fetch_array($result); return $dbarray;}function getStarvation(){  $q = "SELECT * FROM " . TB_PREFIX . "vdata where starv != 0 and owner != 3";  $result = mysql_query($q, $this->connection);  return $this->mysql_fetch_all($result);}function getActivateField($ref, $field, $mode) { if(!$mode) {  $q = "SELECT $field FROM " . TB_PREFIX . "activate where id = '$ref'"; } else {  $q = "SELECT $field FROM " . TB_PREFIX . "activate where username = '$ref'"; } $result = mysql_query($q, $this->connection) or die(mysql_error()); $dbarray = mysql_fetch_array($result); return $dbarray[$field];}function login($username, $password) { $q = "SELECT password,sessid FROM " . TB_PREFIX . "users where username = '$username'"; $result = mysql_query($q, $this->connection); $dbarray = mysql_fetch_array($result); if($dbarray['password'] == md5($password)) {  return true; } else {  return false; }}function checkActivate($act) { $q = "SELECT * FROM " . TB_PREFIX . "activate where act = '$act'"; $result = mysql_query($q, $this->connection); $dbarray = mysql_fetch_array($result); return $dbarray;}function sitterLogin($username, $password) { $q = "SELECT sit1,sit2 FROM " . TB_PREFIX . "users where username = '$username' and access != " . BANNED; $result = mysql_query($q, $this->connection); $dbarray = mysql_fetch_array($result); if($dbarray['sit1'] != 0) {  $q2 = "SELECT password FROM " . TB_PREFIX . "users where id = " . $dbarray['sit1'] . " and access != " . BANNED;  $result2 = mysql_query($q2, $this->connection);  $dbarray2 = mysql_fetch_array($result2); } if($dbarray['sit2'] != 0) {   $q3 = "SELECT password FROM " . TB_PREFIX . "users where id = " . $dbarray['sit2'] . " and access != " . BANNED;   $result3 = mysql_query($q3, $this->connection);   $dbarray3 = mysql_fetch_array($result3); } if($dbarray['sit1'] != 0 || $dbarray['sit2'] != 0) {  if($dbarray2['password'] == md5($password) || $dbarray3['password'] == md5($password)) {   return true;  } else {   return false;  } } else {  return false; }}function setDeleting($uid, $mode) { $time = time() + 72 * 3600; if(!$mode) {  $q = "INSERT into " . TB_PREFIX . "deleting values ($uid,$time)"; } else {  $q = "DELETE FROM " . TB_PREFIX . "deleting where uid = $uid"; } mysql_query($q, $this->connection);}function isDeleting($uid) { $q = "SELECT timestamp from " . TB_PREFIX . "deleting where uid = $uid"; $result = mysql_query($q, $this->connection); $dbarray = mysql_fetch_array($result); return $dbarray['timestamp'];}function modifyGold($userid, $amt, $mode) { if(!$mode) {  $q = "UPDATE " . TB_PREFIX . "users set gold = gold - $amt where id = $userid"; } else {  $q = "UPDATE " . TB_PREFIX . "users set gold = gold + $amt where id = $userid"; } return mysql_query($q, $this->connection);}/*****************************************Function to retrieve user array via Username or IDMode 0: Search by UsernameMode 1: Search by IDReferences: Alliance ID*****************************************/function getUserArray($ref, $mode) { if(!$mode) {  $q = "SELECT * FROM " . TB_PREFIX . "users where username = '$ref'"; } else {  $q = "SELECT * FROM " . TB_PREFIX . "users where id = $ref"; } $result = mysql_query($q, $this->connection); return mysql_fetch_array($result);}function activeModify($username, $mode) { $time = time(); if(!$mode) {  $q = "INSERT into " . TB_PREFIX . "active VALUES ('$username',$time)"; } else {  $q = "DELETE FROM " . TB_PREFIX . "active where username = '$username'"; } return mysql_query($q, $this->connection);}function addActiveUser($username, $time) { $q = "REPLACE into " . TB_PREFIX . "active values ('$username',$time)"; if(mysql_query($q, $this->connection)) {  return true; } else {  return false; }}function updateActiveUser($username, $time) { $q = "REPLACE into " . TB_PREFIX . "active values ('$username',$time)"; $q2 = "UPDATE " . TB_PREFIX . "users set timestamp = $time where username = '$username'"; $exec1 = mysql_query($q, $this->connection); $exec2 = mysql_query($q2, $this->connection); if($exec1 && $exec2) {  return true; } else {  return false; }}function checkactiveSession($username, $sessid) { $q = "SELECT username FROM " . TB_PREFIX . "users where username = '$username' and sessid = '$sessid' LIMIT 1"; $result = mysql_query($q, $this->connection); if(mysql_num_rows($result) != 0) {  return true; } else {  return false; }}function submitProfile($uid, $gender, $location, $birthday, $des1, $des2) { $q = "UPDATE " . TB_PREFIX . "users set gender = $gender, location = '$location', birthday = '$birthday', desc1 = '$des1', desc2 = '$des2' where id = $uid"; return mysql_query($q, $this->connection);}function gpack($uid, $gpack) { $q = "UPDATE " . TB_PREFIX . "users set gpack = '$gpack' where id = $uid"; return mysql_query($q, $this->connection);}function GetOnline($uid) { $q = "SELECT sit FROM " . TB_PREFIX . "online where uid = $uid"; $result = mysql_query($q, $this->connection); $dbarray = mysql_fetch_array($result); return $dbarray['sit'];}function UpdateOnline($mode, $name = "", $time = "", $uid = 0) { global $session; if($mode == "login") {  $q = "INSERT IGNORE INTO " . TB_PREFIX . "online (name, uid, time, sit) VALUES ('$name', '$uid', " . time() . ", 0)";  return mysql_query($q, $this->connection); } else if($mode == "sitter") {  $q = "INSERT IGNORE INTO " . TB_PREFIX . "online (name, uid, time, sit) VALUES ('$name', '$uid', " . time() . ", 1)";  return mysql_query($q, $this->connection); } else {  $q = "DELETE FROM " . TB_PREFIX . "online WHERE name ='" . addslashes($session->username) . "'";  return mysql_query($q, $this->connection); }}function generateBase($sector) {switch($sector) {case 1:$q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x < 0 and y > 0 and occupied = 0";break;case 2:$q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x > 0 and y > 0 and occupied = 0";break;case 3:$q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x < 0 and y < 0 and occupied = 0";break;case 4:$q = "Select * from ".TB_PREFIX."wdata where fieldtype = 3 and x > 0 and y < 0 and occupied = 0";break;}$result = mysql_query($q, $this->connection);$num_rows = mysql_num_rows($result);$result = $this->mysql_fetch_all($result);$base = rand(0, ($num_rows-1));return $result[$base]['id'];}function setFieldTaken($id) { $q = "UPDATE " . TB_PREFIX . "wdata set occupied = 1 where id = $id"; return mysql_query($q, $this->connection);}function addVillage($wid, $uid, $username, $capital) { $total = count($this->getVillagesID($uid)); if($total >= 1) {  $vname = $username . "\'s village " . ($total + 1); } else {  $vname = $username . "\'s village"; } $time = time(); $q = "INSERT into " . TB_PREFIX . "vdata (wref, owner, name, capital, pop, cp, celebration, wood, clay, iron, maxstore, crop, maxcrop, lastupdate, created) values ('$wid', '$uid', '$vname', '$capital', 2, 1, 0, 750, 750, 750, ".STORAGE_BASE.", 750, ".STORAGE_BASE.", '$time', '$time')"; return mysql_query($q, $this->connection) or die(mysql_error());}function addResourceFields($vid, $type) { switch($type) {  case 1:   $q = "INSERT into " . TB_PREFIX . "fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,4,4,1,4,4,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";   break;  case 2:   $q = "INSERT into " . TB_PREFIX . "fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,3,4,1,3,2,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";   break;  case 3:   $q = "INSERT into " . TB_PREFIX . "fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,1,4,1,3,2,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";   break;  case 4:   $q = "INSERT into " . TB_PREFIX . "fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,1,4,1,2,2,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";   break;  case 5:   $q = "INSERT into " . TB_PREFIX . "fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,1,4,1,3,1,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";   break;  case 6:   $q = "INSERT into " . TB_PREFIX . "fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,4,4,1,3,4,4,4,4,4,4,4,4,4,4,4,2,4,4,1,15)";   break;  case 7:   $q = "INSERT into " . TB_PREFIX . "fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,1,4,4,1,2,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";   break;  case 8:   $q = "INSERT into " . TB_PREFIX . "fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,3,4,4,1,2,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";   break;  case 9:   $q = "INSERT into " . TB_PREFIX . "fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,3,4,4,1,1,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";   break;  case 10:   $q = "INSERT into " . TB_PREFIX . "fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,3,4,1,2,2,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";   break;  case 11:   $q = "INSERT into " . TB_PREFIX . "fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,3,1,1,3,1,4,4,3,3,2,2,3,1,4,4,2,4,4,1,15)";   break;  case 12:   $q = "INSERT into " . TB_PREFIX . "fdata (vref,f1t,f2t,f3t,f4t,f5t,f6t,f7t,f8t,f9t,f10t,f11t,f12t,f13t,f14t,f15t,f16t,f17t,f18t,f26,f26t) values($vid,1,4,1,1,2,2,3,4,4,3,3,4,4,1,4,2,1,2,1,15)";   break; } return mysql_query($q, $this->connection);
Ссылка на комментарий
Поделиться на другие сайты

Для публикации сообщений создайте учётную запись или авторизуйтесь

Вы должны быть пользователем, чтобы оставить комментарий

Создать учетную запись

Зарегистрируйте новую учётную запись в нашем сообществе. Это очень просто!

Регистрация нового пользователя

Войти

Уже есть аккаунт? Войти в систему.

Войти
  • Последние посетители   0 пользователей онлайн

    • Ни одного зарегистрированного пользователя не просматривает данную страницу
×
×
  • Создать...