Mysqli does not display results

Asked

Viewed 1,450 times

1

I have the following table in the comic DUELOS , I entered the data but at the time of picking up the result I’m having problem what should be?

data in table inserir a descrição da imagem aqui

code that fetches the data

$iddesafiante = $_SESSION['user_id'];
$desafiante = $_SESSION['username'];
$iddesafiado = $_GET['idd'];
$desafiado = $_GET['d']; 
$sql = "SELECT * FROM duelos WHERE status='desafiado' AND desafiado='$desafiante' ";
 $query = $mysqli->query($sql);
 while ($dados = $query->mysqli_fetch_array()) {
 echo "Você foi desafiado por: ' . $dados['iddesafiado'] . ' <br> Aceitar Desafio / Não aceitar";
 }

error generated Fatal error: Call to undefined method mysqli_result::mysqli_fetch_array

code of the complete page

  <?php
    require_once("functions.php");
    require_once("config2.php");
    session_start();
    if (logged_in() == false) {
        redirect_to("login.php");
    }

    //values to be inserted in database table
    $time = time();
    $acao = $_GET['acao'];
    $iddesafiante = $_SESSION['user_id'];
    $desafiante = $_SESSION['username'];
    $iddesafiado = $_GET['idd'];
    $desafiado = $_GET['d'];
    $status = 'desafiado';

    switch($acao)
    {
        case 'nenhuma';
           // ---------------- VERIFICACAO SE FOI DESAFIADO------------------
    $sql = "SELECT * FROM duelos WHERE status='desafiado' AND desafiado='$desafiante' ";
    $query = $mysqli->query($sql);
    while ($dados = $query->mysqli_fetch_array()) {
      echo "Você foi desafiado por: ' . $dados['iddesafiado'] . ' <br> Aceitar Desafio / Não aceitar";
    }

        break;

        case 'desafiar';
           // ---------------- VERIFICACAO SE JA DUELOU NAS ULTIMAS 24 HORAS COM O MESMO OPONENTE ------------------

           // ---------------- SE NAO DUELOU DESAFIAR ------------------


        break;

        case 'aceitar';
                // ---------------- UPDATE ACEITAR ------------------

        break;

        case 'emduelo';
      echo " breve";
        break;
        case 'breve';
            echo 'verificar se tem desafio';
        break;
        case 'aguardando';
            echo 'vreve';
        break;

        default;
        echo 'texto quando nao existir o parametro';
        break;
    }

    ?>
  • tries to trade $dados['iddesafiado'] for $dados[5] and $query->mysqli_fetch_array() for $query->fetch_array()

  • unsuccessfully altered

2 answers

1

You’re invoking a non-existent method:

while ($data = $query->mysqli_fetch_array()) {

Correct for

while ($dados = $query->fetch_array()) {

Of course, it doesn’t mean it will bring the result you expect because the way you are generating the query query can be inconsistent and insecure.

Also check that the query is being formed correctly.

$sql = "SELECT * FROM duelos WHERE status='desafiado' AND desafiado='$desafiante' ";

Create a simple breackpoint to check how the query is being mounted.

Example:

$sql = "SELECT * FROM duelos WHERE status='desafiado' AND desafiado='$desafiante' ";
echo $sql; exit;

This will print the query on the screen and stop the page from running. This is just to debug and find the problem. This can be called breakpoint.

What should you do? Just read the query and evaluate if it is correct.

  • Parse error: syntax error, Unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in line 28 = echo "You have been challenged by: '.$data['defiant']. ' <br> Accept Challenge / Do not accept"; !!! I added echo $sql; Exit; and still showed nothing

  • In the comments you made, you can see that there are several other errors, not necessarily related to the problem described in the question. These are various problems that make the question too wide. The main problem concerning Mysqli is solved. Other specific problems, open a new question or search the site if you have no similar question.

0

One way I was using is the following :

while ($a = mysqli_fetch_assoc($exec)) { // fetch_assoc obtendo todos os resultados do banco de dados conforme a consulta
                foreach ($a as $i => $rows) {
                    echo $rows = $a[$i] . "<br/>";
                    $i++;
                }
            }

Browser other questions tagged

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