fix: counting queries optimization

Closes #300
This commit is contained in:
Martin Ambrus
2017-11-04 15:41:35 +01:00
parent 59e6a309e9
commit a822504706
18 changed files with 131 additions and 122 deletions
+2 -1
View File
@@ -28,7 +28,8 @@ $sessionaccess = $access['access'];
if($sessionaccess != 9) die("<h1><font color=\"red\">Access Denied: You are not Admin!</font></h1>");
$users = mysqli_num_rows(mysqli_query($GLOBALS["link"], "SELECT * FROM ".TB_PREFIX."users"));
$users = mysqli_fetch_array(mysqli_query($GLOBALS["link"], "SELECT Count(*) as Total FROM ".TB_PREFIX."users"), MYSQLI_ASSOC);
$users = $users['Total'];
$reason = $_POST['unbanreason'];
$admin = $session;
+8 -7
View File
@@ -318,13 +318,14 @@ class adm_DB {
function DelVillage($wref, $mode=0){
global $database;
$wref = (int) $wref;
if($mode==0){
$q = "SELECT * FROM ".TB_PREFIX."vdata WHERE `wref` = $wref and capital = 0";
}else{
$q = "SELECT * FROM ".TB_PREFIX."vdata WHERE `wref` = $wref";
}
$result = mysqli_query($this->connection, $q);
if(mysqli_num_rows($result) > 0){
if ($mode==0) {
$q = "SELECT Count(*) as Total FROM ".TB_PREFIX."vdata WHERE `wref` = $wref and capital = 0";
} else {
$q = "SELECT Count(*) as Total FROM ".TB_PREFIX."vdata WHERE `wref` = $wref";
}
$result = mysqli_fetch_array(mysqli_query($this->connection, $q), MYSQLI_ASSOC);
if($result['Total'] > 0){
mysqli_query($this->connection,"Insert into ".TB_PREFIX."admin_log values (0,".(int) $_SESSION['id'].",'Deleted village <b>$wref</b>',".time().")");
$database->clearExpansionSlot($wref);
+3 -5
View File
@@ -30,14 +30,12 @@ class Automation {
private $bountyOpop = 1;
public function isWinner() {
$q = mysqli_query($GLOBALS['link'],"SELECT vref FROM ".TB_PREFIX."fdata WHERE f99 = '100' and f99t = '40'");
$isThere = mysqli_num_rows($q);
if($isThere > 0)
// check whether someone already built a level 100 Wonder of the World
$q = mysqli_fetch_array(mysqli_query($GLOBALS['link'],"SELECT Count(*) as Total FROM ".TB_PREFIX."fdata WHERE f99 = 100 and f99t = 40"), MYSQLI_ASSOC);
if($q['Total'] > 0)
{
header('Location: winner.php');
exit;
}else{
## there is no winner
}
}
+61 -60
View File
@@ -652,9 +652,9 @@ class MYSQLi_DB implements IDbConnection {
function checkactiveSession($username, $sessid) {
list($username, $sessid) = $this->escape_input($username, $sessid);
$q = "SELECT username FROM " . TB_PREFIX . "users where username = '$username' and sessid = '$sessid' LIMIT 1";
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result) != 0) {
$q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "users where username = '$username' and sessid = '$sessid' LIMIT 1";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if($result['Total'] > 0) {
return true;
} else {
return false;
@@ -1208,9 +1208,9 @@ class MYSQLi_DB implements IDbConnection {
function CheckForum($id) {
list($id) = $this->escape_input((int) $id);
$q = "SELECT * from " . TB_PREFIX . "forum_cat where alliance = '$id'";
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result)) {
$q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "forum_cat where alliance = $id";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if ($result['Total']) {
return true;
} else {
return false;
@@ -1237,9 +1237,9 @@ class MYSQLi_DB implements IDbConnection {
function CheckLastTopic($id) {
list($id) = $this->escape_input($id);
$q = "SELECT * from " . TB_PREFIX . "forum_topic where cat = '$id'";
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result)) {
$q = "SELECT Count(*) as Total from " . TB_PREFIX . "forum_topic where cat = $id";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if($result['Total']) {
return true;
} else {
return false;
@@ -1249,9 +1249,9 @@ class MYSQLi_DB implements IDbConnection {
function CheckLastPost($id) {
list($id) = $this->escape_input($id);
$q = "SELECT * from " . TB_PREFIX . "forum_post where topic = '$id'";
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result)) {
$q = "SELECT Count(*) as Total from " . TB_PREFIX . "forum_post where topic = $id";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if ($result['Total']) {
return true;
} else {
return false;
@@ -1325,9 +1325,9 @@ class MYSQLi_DB implements IDbConnection {
function CheckCatTopic($id) {
list($id) = $this->escape_input($id);
$q = "SELECT * from " . TB_PREFIX . "forum_topic where cat = '$id'";
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result)) {
$q = "SELECT Count(*) as Total from " . TB_PREFIX . "forum_topic where cat = $id";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if ($result['Total']) {
return true;
} else {
return false;
@@ -1337,9 +1337,9 @@ class MYSQLi_DB implements IDbConnection {
function CheckResultEdit($alli) {
list($alli) = $this->escape_input($alli);
$q = "SELECT * from " . TB_PREFIX . "forum_edit where alliance = '$alli'";
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result)) {
$q = "SELECT Count(*) as Total from " . TB_PREFIX . "forum_edit where alliance = $alli";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if ($result['Total']) {
return true;
} else {
return false;
@@ -1409,9 +1409,9 @@ class MYSQLi_DB implements IDbConnection {
function checkVilExist($wref) {
list($wref) = $this->escape_input((int) $wref);
$q = "SELECT * FROM " . TB_PREFIX . "vdata where wref = '$wref'";
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result)) {
$q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "vdata where wref = '$wref'";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if ($result['Total']) {
return true;
} else {
return false;
@@ -1421,9 +1421,9 @@ class MYSQLi_DB implements IDbConnection {
function checkOasisExist($wref) {
list($wref) = $this->escape_input((int) $wref);
$q = "SELECT * FROM " . TB_PREFIX . "odata where wref = '$wref'";
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result)) {
$q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "odata where wref = '$wref'";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if($result['Total']) {
return true;
} else {
return false;
@@ -1530,9 +1530,9 @@ class MYSQLi_DB implements IDbConnection {
function checkSurvey($topic) {
list($topic) = $this->escape_input((int) $topic);
$q = "SELECT * FROM " . TB_PREFIX . "forum_survey where topic = $topic";
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result)) {
$q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "forum_survey where topic = $topic";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if ($result['Total']) {
return true;
} else {
return false;
@@ -3293,9 +3293,9 @@ class MYSQLi_DB implements IDbConnection {
function getBuildingByField2($wid,$field) {
list($wid,$field) = $this->escape_input((int) $wid,(int) $field);
$q = "SELECT * FROM " . TB_PREFIX . "bdata where wid = $wid and field = $field and master = 0";
$result = mysqli_query($this->dblink,$q);
return mysqli_num_rows($result);
$q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "bdata where wid = $wid and field = $field and master = 0";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
return $result['Total'];
}
function getBuildingByType($wid,$type) {
@@ -3309,9 +3309,9 @@ class MYSQLi_DB implements IDbConnection {
function getBuildingByType2($wid,$type) {
list($wid,$type) = $this->escape_input((int) $wid,(int) $type);
$q = "SELECT * FROM " . TB_PREFIX . "bdata where wid = $wid and type = $type and master = 0";
$result = mysqli_query($this->dblink,$q);
return mysqli_num_rows($result);
$q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "bdata where wid = $wid and type = $type and master = 0";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
return $result['Total'];
}
function getDorf1Building($wid) {
@@ -4133,9 +4133,9 @@ class MYSQLi_DB implements IDbConnection {
***************************/
function getWW() {
$q = "SELECT * FROM " . TB_PREFIX . "fdata WHERE f99t = 40";
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result)) {
$q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "fdata WHERE f99t = 40";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if($result['Total']) {
return true;
} else {
return false;
@@ -5123,9 +5123,9 @@ References:
function getHeroDead($id) {
list($id) = $this->escape_input((int) $id);
$q = "SELECT dead FROM " . TB_PREFIX . "hero WHERE `uid` = $id AND dead = 0";
$result = mysqli_query($this->dblink,$q);
if (mysqli_num_rows($result) > 0) {
$q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "hero WHERE `uid` = $id AND dead = 0";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if ($result['Total'] > 0) {
return 0;
} else {
return 1;
@@ -5140,9 +5140,9 @@ References:
function getHeroInRevive($id) {
list($id) = $this->escape_input((int) $id);
$q = "SELECT inrevive FROM " . TB_PREFIX . "hero WHERE `uid` = $id AND inrevive = 1";
$result = mysqli_query($this->dblink,$q);
if (mysqli_num_rows($result) > 0) {
$q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "hero WHERE `uid` = $id AND inrevive = 1";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if ($result['Total'] > 0) {
return 1;
} else {
return 0;
@@ -5157,9 +5157,9 @@ References:
function getHeroInTraining($id) {
list($id) = $this->escape_input((int) $id);
$q = "SELECT intraining FROM " . TB_PREFIX . "hero WHERE `uid` = $id AND intraining = 1";
$result = mysqli_query($this->dblink,$q);
if (mysqli_num_rows($result) > 0) {
$q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "hero WHERE `uid` = $id AND intraining = 1";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if ($result['Total'] > 0) {
return 1;
} else {
return 0;
@@ -5300,15 +5300,16 @@ References:
***************************/
function checkAttack($wref, $toWref) {
list($wref, $toWref) = $this->escape_input((int) $wref, (int) $toWref);
$q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement.from = $wref and " . TB_PREFIX . "movement.to = $toWref and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 and (" . TB_PREFIX . "attacks.attack_type = 3 or " . TB_PREFIX . "attacks.attack_type = 4) ORDER BY endtime ASC";
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result)) {
return true;
} else {
return false;
}
}
list($wref, $toWref) = $this->escape_input((int) $wref, (int) $toWref);
$q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement.from = $wref and " . TB_PREFIX . "movement.to = $toWref and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 and (" . TB_PREFIX . "attacks.attack_type = 3 or " . TB_PREFIX . "attacks.attack_type = 4) ORDER BY endtime ASC";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if ($result['Total']) {
return true;
} else {
return false;
}
}
/***************************
Function checkEnforce
@@ -5318,9 +5319,9 @@ References:
function checkEnforce($wref, $toWref) {
list($wref, $toWref) = $this->escape_input((int) $wref, (int) $toWref);
$q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement.from = $wref and " . TB_PREFIX . "movement.to = $toWref and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 and " . TB_PREFIX . "attacks.attack_type = 2 ORDER BY endtime ASC";
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result)) {
$q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement.from = $wref and " . TB_PREFIX . "movement.to = $toWref and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 and " . TB_PREFIX . "attacks.attack_type = 2 ORDER BY endtime ASC";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if($result['Total']) {
return true;
}else{
return false;
@@ -5335,9 +5336,9 @@ References:
function checkScout($wref, $toWref) {
list($wref, $toWref) = $this->escape_input((int) $wref, (int) $toWref);
$q = "SELECT * FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement.from = $wref and " . TB_PREFIX . "movement.to = $toWref and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 and " . TB_PREFIX . "attacks.attack_type = 1 ORDER BY endtime ASC";
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result)) {
$q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "movement, " . TB_PREFIX . "attacks where " . TB_PREFIX . "movement.from = $wref and " . TB_PREFIX . "movement.to = $toWref and " . TB_PREFIX . "movement.ref = " . TB_PREFIX . "attacks.id and " . TB_PREFIX . "movement.proc = 0 and " . TB_PREFIX . "movement.sort_type = 3 and " . TB_PREFIX . "attacks.attack_type = 1 ORDER BY endtime ASC";
$result = mysqli_fetch_array(mysqli_query($this->dblink,$q), MYSQLI_ASSOC);
if($result['Total']) {
return true;
}else{
return false;
+6 -6
View File
@@ -335,9 +335,9 @@ class Message {
// Vulnerability closed by Shadow
$q = "SELECT * FROM ".TB_PREFIX."mdata WHERE owner='".$session->uid."' AND time > ".(time() - 60);
$res = mysqli_query($GLOBALS['link'],$q) or die(mysqli_error($database->dblink). " query ".$q);
$flood = mysqli_num_rows($res);
$q = "SELECT Count(*) as Total FROM ".TB_PREFIX."mdata WHERE owner='".$session->uid."' AND time > ".(time() - 60);
$res = mysqli_fetch_array(mysqli_query($GLOBALS['link'],$q) or die(mysqli_error($database->dblink). " query ".$q), MYSQLI_ASSOC);
$flood = $res['Total'];
if($flood > 5)
return; //flood
@@ -425,9 +425,9 @@ class Message {
// Vulnerability closed by Shadow
if ($security_check) {
$q = "SELECT * FROM ".TB_PREFIX."mdata WHERE owner='".$session->uid."' AND time > ".(time() - 60);
$res = mysqli_query($GLOBALS['link'],$q) or die(mysqli_error($database->dblink). " query ".$q);
$flood = mysqli_num_rows($res);
$q = "SELECT Count(*) as Total FROM ".TB_PREFIX."mdata WHERE owner='".$session->uid."' AND time > ".(time() - 60);
$res = mysqli_fetch_array(mysqli_query($GLOBALS['link'],$q) or die(mysqli_error($database->dblink). " query ".$q), MYSQLI_ASSOC);
$flood = $res['Total'];
if($flood > 5)
return; //flood
}
+3 -2
View File
@@ -20,8 +20,9 @@
public function getUserRank($id) {
$ranking = $this->getRank();
$users = "SELECT * FROM " . TB_PREFIX . "users WHERE access < " . (INCLUDE_ADMIN ? "10" : "8") . "";
$users2 = mysqli_num_rows(mysqli_query($GLOBALS['link'],$users));
$users = "SELECT Count(*) as Total FROM " . TB_PREFIX . "users WHERE access < " . (INCLUDE_ADMIN ? "10" : "8");
$users2 = mysqli_fetch_array(mysqli_query($GLOBALS['link'],$users), MYSQLI_ASSOC);
$users2 = $users2['Total'];
$users3 = $users2+1;
$myrank = 0;
if(count($ranking) > 0) {
+2 -2
View File
@@ -443,8 +443,8 @@ if($session->access != BANNED){
// If is a WW village you can target on WW , if is not a WW village catapults will target randomly.
// Like it says : Exceptions are the WW which can always be targeted and the treasure chamber which can always be targeted, except with the unique artifact.
// Fixed by Advocaite and Shadow
$q = mysqli_query($GLOBALS['link'],"SELECT vref FROM ".TB_PREFIX."fdata WHERE f99t = '40' AND vref = ".(int) $data['to_vid']."");
$isThere = mysqli_num_rows($q);
$q = mysqli_fetch_array(mysqli_query($GLOBALS['link'],"SELECT Count(*) as Total FROM ".TB_PREFIX."fdata WHERE f99t = '40' AND vref = ".(int) $data['to_vid']), MYSQLI_ASSOC);
$isThere = $q['Total'];
if($isThere > 0)
{
$iswwvilla = 1;