Problem with using Else

Asked

Viewed 37 times

0

Good evening, I’m having trouble that is the following, on the requested page if the user has 1 order or more it displays the request, if the user has no request appears the message "You have no request", or at least should appear. With the code I have so far, when the user has some request appears normally, but when he has not made any request, the message does not appear. I’ll leave my code below.

  $result=$conn->query($comando);
            $result = mysqli_query($conn, $comando);
if (count($result)>0){
    while ($row = mysqli_fetch_assoc($result)){ ?>
        <tr>
            <th scope="row"><?= $row['order_id'] ?></th>
            <td><?= $row['order_date'] ?></td>
            <td><?= $row['order_name'] ?></td>
            <td><?= $row['order_endereco'] ?></td>
            <td><?= $row['order_numero'] ?></td>
            <td><?= $row['order_referencia'] ?></td>
            <td><?= $row['situacao'] ?></td>
        </tr>
<?php } }else{ ?>
<div class="text-center">Você não possui nenhum pedido</div>
<?php  } ?>

2 answers

0


That’s because $result is not an array, it is an object of the type mysqli_result, then it makes no sense to use count($result) to check if he found the result or not.

You should be checking the property num_rows is different from 0, that is to say

if ($result->num_rows > 0) {
  // Encontrou um ou mais resultados
} else {
  // Não encontrou resultados
}

0

if ($result = mysqli_query($conn, $comando))
{
while ($row = mysqli_fetch_assoc($result)){ ?>
        <tr>
            <th scope="row"><?= $row['order_id'] ?></th>
            <td><?= $row['order_date'] ?></td>
            <td><?= $row['order_name'] ?></td>
            <td><?= $row['order_endereco'] ?></td>
            <td><?= $row['order_numero'] ?></td>
            <td><?= $row['order_referencia'] ?></td>
            <td><?= $row['situacao'] ?></td>
        </tr>
<?php } }else { ?>
<div class="text-center">Você não possui nenhum pedido</div>
<?php  } ?>

Browser other questions tagged

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