Transform result into array and get all values in "select"

2

I'm having a problem when creating a system, I need to do a select:

$select = $mysqli->query("SELECT * FROM 'guild' WHERE 'user_id'='$userid'");
$row = $select->num_rows;
$get = $select->fetch_array();
$guild = $get['guild'];

Transform the variable $guild into one array and get it in the other select

$select = $mysqli->query("SELECT * FROM 'guild' WHERE 'guild' != '$guild' AND 'user_id' != '$userid' ORDER BY 'score' DESC LIMIT 5");
$row = $select->num_rows;
$get = $select->fetch_array();
$guild_name = $get['guild'];
$guild_icn = $get['icn'];
$guild_score = $get['score'];

Exactly what I need is that all select results are removed in this array , but I have no idea how to do it.

Complete code:

<?php
require_once ("includes/connection.php");
require_once ("includes/start-session.php");
require_once ("includes/encript.php");
$ip = $_SERVER["REMOTE_ADDR"];
$iusername = "Trabzera";
$userid = "10";
$select = $mysqli->query("SELECT * FROM 'data' WHERE username='$iusername' AND 'ip'='$ip'");
$row = $select->num_rows;
$get = $select->fetch_array();
if ($row > 0) {
$select = $mysqli->query("SELECT * FROM 'guild' WHERE 'user_id'='$userid'");
$row = $select->num_rows;
$get = $select->fetch_array();
$guild = $get['guild'];
#----------------------
$select = $mysqli->query("SELECT * FROM 'guild' WHERE 'guild' != '$guild' AND 'user_id' != '$userid' ORDER BY 'score' DESC LIMIT 5");
$row = $select->num_rows;
$get = $select->fetch_array();
$guild_name = $get['guild'];
$guild_icn = $get['icn'];
$guild_score = $get['score'];
$score_result = round($guild_score/1000);
$count = 1;
echo '<div id="guild-central">Central da Guild</div>
<div id="guild-central-msg">Selecione abaixo a opção desejada:</div>
<menu id="create-guild">
<div id="create-guild-msg">Criar uma Guild</div>
<div id="create-guild-txt">Para criar uma Guild é necessário alguns requerimentos:</div>
<div id="create-guild-req1">- Nível 10+</div>
<div id="create-guild-req2">- 1000 KP (Knautiluz Points)</div>
<div id="create-guild-reqok">Caso atenda esses requsistos clique abaixo:</div>
<button id="create-guild-button">Criar uma Guild</button>
</menu>';
echo '<menu id="join-guild">
<div id="join-guild-msg">Juntar-se a uma Guild</div>';
#-----------------------------------------------------
if ($row > 0) {
while ($count <= $row) {
echo'
<script>
$("#guild-join-request-'.$count.'").click (function() {
var guildName = $("#guild-join-name-'.$count.'").html();
var src = $("#guild-join-icn-'.$count.'").attr("src");
var iusername = "'.$iusername.'";
var userid = "'.$userid.'";
$.ajax({
url: "systems/join-guild.php",
type: "POST",
data: {iusername: iusername, userid: userid, guildName: guildName, src: src},
beforeSend: function() {
$("#sucess").html("Carregando...");
},
success: function (result) {
$("#user-painel-2").html(result);
} 
});
});
</script>'; 
echo'
<div id="guild-join-'.$count.'" class="guild-join">
<img src="'.$guild_icn.'" id="guild-join-icn-'.$count.'"></img>
<div id="guild-join-name-'.$count.'" class="guild-join-name">'.$guild_name.'</div>
<div id="guild-join-score-'.$count.'" class="guild-join-score" >- '.$score_result.'K</div>
<input style="cursor:pointer;" type="button" id="guild-join-request-'.$count.'" value="Solicitar" class="guild-join-request"></input>
<div id="guild-join-underline-'.$count.'" class="guild-join-underline"></div>
</div>';
$get = $select->fetch_array();
$guild_name = $get['guild'];
$guild_icn = $get['icn'];
$guild_score = $get['score'];
$score_result = round($guild_score/1000);
$count++;
}
echo'<menu id="guild-request">
<div id="guild-request-msg">Solicitações Pendentes:</div>'; 
#---------------------------------------------------------------
$select = $mysqli->query("SELECT * FROM 'guild' WHERE 'user_id' ='$userid'");
$row = $select->num_rows;
$get = $select->fetch_array();
$guild_name = $get['guild'];
$guild_icn = $get['icn'];
$guild_score = $get['score'];
$score_result = round($guild_score/1000);
$count = 1;
if($row > 0) {
while ($count <= $row) {
echo'
<div id="guild-request-'.$count.'" class="guild-request">
<img src="'.$guild_icn.'" id="guild-request-icn-'.$count.'"></img>
<div id="guild-request-name-'.$count.'" class="guild-request-name" >'.$guild_name.'</div>
<div id="guild-request-score-'.$count.'" class="guild-request-score">- '.$score_result.'K</div>
<button id="guild-request-cancel-'.$count.'" class="guild-request-cancel">Cancelar</button>
<div id="guild-request-underline-'.$count.'" class="guild-request-underline"></div>
</div>';
$get = $select->fetch_array();
$guild_name = $get['guild'];
$guild_icn = $get['icn'];
$guild_score = $get['score'];
$score_result = round($guild_score/1000);
$count++;
}
echo '</div>
</menu>';
} else {
echo'   
<menu id="guild-request">
<div id="guild-request-msg">Solicitações Pendentes:</div>
<div id="no-requests">Nenhuma solicitação pendente.</div>
</menu></menu>';    
}
#---------------------------------------------------------------
} else {
echo "Ainda não existe nenhuma guild."; 
}
} else {
echo "Erro.";   
}
    
asked by anonymous 19.12.2016 / 21:19

1 answer

3

Here are some changes that I suggest, so that your code works the way you want.

As already mentioned, fetch_array returns only one query row or NULL if there is nothing else to fill (either by the empty result or by the end result of the rows returned by the database). If we use fetch_all , which returns, by default, all query results in a numeric array, we can do this:

$select = $mysqli->query("SELECT * FROM 'guild' WHERE 'user_id'='$userid'");   
$guilds = $select->fetch_all();
$row = $select->num_rows;
if ($row > 0) {
  $guild = implode("', '", array_column($guilds, "guild"));
  $select = $mysqli->query("SELECT * FROM 'guild' WHERE 'guild' NOT IN('$guild') AND 'user_id' != '$userid' ORDER BY 'score' DESC LIMIT 5");

In this part of the code, we execute the query to paste all the guilds that the user is inserted / pending:

$select = $mysqli->query("SELECT * FROM 'guild' WHERE 'user_id'='$userid'");   
$guilds = $select->fetch_all();

Then we check the amount of result ( if ($row > 0) ), if it is greater than zero, we set the new query to filter the guilds that the user is not inserted / pending. Begin by using the array_column to get only the value that interests us from the result ( guild ) and, together with the implode , we form a comma-separated string with all the guilds that the user belongs to / pending:

$guild = implode("', '", array_column($guilds, "guild"));

For our quary, we will use NOT IN() to filter for the guilds we do not want not resulting:

"SELECT * FROM 'guild' WHERE 'guild' NOT IN('$guild') AND 'user_id' != '$userid' ORDER BY 'score' DESC LIMIT 5"

For the rest I believe you can keep your code like this, using fetch_array . Full Code:

<?php
require_once ("includes/connection.php");
require_once ("includes/start-session.php");
require_once ("includes/encript.php");
$ip = $_SERVER["REMOTE_ADDR"];
$iusername = "Trabzera";
$userid = "10";
$select = $mysqli->query("SELECT * FROM 'data' WHERE username='$iusername' AND 'ip'='$ip'");
$row = $select->num_rows;
$get = $select->fetch_array();
if ($row > 0) {
$select = $mysqli->query("SELECT * FROM 'guild' WHERE 'user_id'='$userid'");   
$guilds = $select->fetch_all();
$row = $select->num_rows;
if ($row > 0) {
  $guild = implode("', '", array_column($guilds, "guild"));
  $select = $mysqli->query("SELECT * FROM 'guild' WHERE 'guild' NOT IN('$guild') AND 'user_id' != '$userid' ORDER BY 'score' DESC LIMIT 5");
  $row = $select->num_rows;
  $get = $select->fetch_array();
  $guild_name = $get['guild'];
  $guild_icn = $get['icn'];
  $guild_score = $get['score'];
  $score_result = round($guild_score/1000);
  $count = 1;
}
echo '<div id="guild-central">Central da Guild</div>
<div id="guild-central-msg">Selecione abaixo a opção desejada:</div>
<menu id="create-guild">
<div id="create-guild-msg">Criar uma Guild</div>
<div id="create-guild-txt">Para criar uma Guild é necessário alguns requerimentos:</div>
<div id="create-guild-req1">- Nível 10+</div>
<div id="create-guild-req2">- 1000 KP (Knautiluz Points)</div>
<div id="create-guild-reqok">Caso atenda esses requsistos clique abaixo:</div>
<button id="create-guild-button">Criar uma Guild</button>
</menu>';
echo '<menu id="join-guild">
<div id="join-guild-msg">Juntar-se a uma Guild</div>';
#-----------------------------------------------------
if ($row > 0) {
while ($count <= $row) {
echo'
<script>
$("#guild-join-request-'.$count.'").click (function() {
var guildName = $("#guild-join-name-'.$count.'").html();
var src = $("#guild-join-icn-'.$count.'").attr("src");
var iusername = "'.$iusername.'";
var userid = "'.$userid.'";
$.ajax({
url: "systems/join-guild.php",
type: "POST",
data: {iusername: iusername, userid: userid, guildName: guildName, src: src},
beforeSend: function() {
$("#sucess").html("Carregando...");
},
success: function (result) {
$("#user-painel-2").html(result);
} 
});
});
</script>'; 
echo'
<div id="guild-join-'.$count.'" class="guild-join">
<img src="'.$guild_icn.'" id="guild-join-icn-'.$count.'"></img>
<div id="guild-join-name-'.$count.'" class="guild-join-name">'.$guild_name.'</div>
<div id="guild-join-score-'.$count.'" class="guild-join-score" >- '.$score_result.'K</div>
<input style="cursor:pointer;" type="button" id="guild-join-request-'.$count.'" value="Solicitar" class="guild-join-request"></input>
<div id="guild-join-underline-'.$count.'" class="guild-join-underline"></div>
</div>';
$get = $select->fetch_array();
$guild_name = $get['guild'];
$guild_icn = $get['icn'];
$guild_score = $get['score'];
$score_result = round($guild_score/1000);
$count++;
}
echo'<menu id="guild-request">
<div id="guild-request-msg">Solicitações Pendentes:</div>'; 
#---------------------------------------------------------------
$select = $mysqli->query("SELECT * FROM 'guild' WHERE 'user_id' ='$userid'");
$row = $select->num_rows;
$get = $select->fetch_array();
$guild_name = $get['guild'];
$guild_icn = $get['icn'];
$guild_score = $get['score'];
$score_result = round($guild_score/1000);
$count = 1;
if($row > 0) {
while ($count <= $row) {
echo'
<div id="guild-request-'.$count.'" class="guild-request">
<img src="'.$guild_icn.'" id="guild-request-icn-'.$count.'"></img>
<div id="guild-request-name-'.$count.'" class="guild-request-name" >'.$guild_name.'</div>
<div id="guild-request-score-'.$count.'" class="guild-request-score">- '.$score_result.'K</div>
<button id="guild-request-cancel-'.$count.'" class="guild-request-cancel">Cancelar</button>
<div id="guild-request-underline-'.$count.'" class="guild-request-underline"></div>
</div>';
$get = $select->fetch_array();
$guild_name = $get['guild'];
$guild_icn = $get['icn'];
$guild_score = $get['score'];
$score_result = round($guild_score/1000);
$count++;
}
echo '</div>
</menu>';
} else {
echo'   
<menu id="guild-request">
<div id="guild-request-msg">Solicitações Pendentes:</div>
<div id="no-requests">Nenhuma solicitação pendente.</div>
</menu></menu>';    
}
#---------------------------------------------------------------
} else {
echo "Ainda não existe nenhuma guild."; 
}
} else {
echo "Erro.";   
}

Update

If you have problems with the fetch_all method (either by the absence of the native MySQL driver on your server or by the PHP version), you can walk through the result with fetch_assoc or fetch_array itself:

$select = $mysqli->query("SELECT * FROM 'guild' WHERE 'user_id'='$userid'");   
$guilds = [];
while ($guild = $select->fetch_array()) {
  $guilds[] = $guild['guild'];
}
if ($row > 0) {
  $guild = implode("', '", array_column($guilds, "guild"));
  $select = $mysqli->query("SELECT * FROM 'guild' WHERE 'guild' NOT IN('$guild') AND 'user_id' != '$userid' ORDER BY 'score' DESC LIMIT 5");

I hope I have helped.

    
20.12.2016 / 00:02