Converting a result from mysqli_query() to String

Asked

Viewed 444 times

-3

First, I’m not good at writing, but I’ll try to explain the problem

I need to print only one cell (amount of votes already taken) of the query result table, but I am unable to access the value of the conversion error.

i was having to make the query and echo straight into the function call in html

The votes need this after the name description

function pegarQtdVotos($conexao,$classificacao){
    $query = "select qtd_votos from VOTOS where id={$classificacao}";
    $resultado = mysqli_query($conexao, $query);
    $qtd = mysqli_fetch_assoc($resultado);
    return $qtd;
}

<p class="alert-success"><input type="radio" name="classificacao" id="classificacao" value="1"> Execelente</p>
<p class="alert-info"><input type="radio" name="classificacao" id="classificacao" value="2"> Bom </p>
<p class="alert-warning"><input type="radio" name="classificacao" id="classificacao" value="3"> Regular </p>
<p class="alert-danger"><input type="radio" name="classificacao" id="classificacao" value="4"> Ruim </p>
                                        

  • $query = "select qtd_votes from VOTES Where id={$classification}"; $result = mysqli_query($connection, $query); while ($Rows = mysqli_fetch_assoc($result)) { echo $Rows['qtd_votes'];};

  • It worked, thanks... I wanted to do this while on the other page, but when I already print inside the function it worked... thanks... @Leocaracciolo puts the code in the answer to the question

1 answer

3


If you print inside the function will work!

function pegarQtdVotos($conexao,$classificacao){

   $query="select qtd_votos from VOTOS where id={$classificacao}"; 
   $resultado=mysqli_query($conexao,$query);

   $qtd = mysqli_fetch_assoc($resultado);

   //return $qtd;    
   echo $qtd["qtd_votos"];

}

The while is the simplest repetition structure in PHP. With it we inform that a code block must be repeated while the declared condition is true.

$query = "select qtd_votos from VOTOS where id={$classificacao}"; 
$resultado = mysqli_query($conexao, $query); 
 while ($rows = mysqli_fetch_assoc($resultado)) { 
   echo $rows['qtd_votos'];
}; 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.