Always equal table check

Asked

Viewed 47 times

0

I have the following code:

<?PHP
$status = mysqli_query($connecta, "SELECT `online` FROM `char` WHERE `name`='[ADM] koda'");
$resultado = mysqli_num_rows($status);

echo '
<tr align="center" id="player">

<td align="center" bgcolor="#FFFFFF">posicao</td>
<td align="center" bgcolor="#FFFFFF">name</td>
<td align="center" bgcolor="#FFFFFF">guilda</td>';
if($resultado == 0) { 
echo '<td align="center" bgcolor="#FFFFFF">off</td></tr>';
}
if ($resultado == 1) {
echo '<td align="center" bgcolor="#FFFFFF">on</td></tr>';
}
  ?>

Problem: Even with the query with the value in the database at 0, it still shows echo 'on'. The query in SQL returns correctly,the problem is logic in the IF. Does anyone know any solution?

2 answers

3


The function mysqli_num_rows returns the number of query records, not the select field.

You need to check whether the outworking of the consultation is equal to 0:

$status = mysqli_query($connecta, "SELECT `online` FROM `char` WHERE `name`='[ADM] koda'");
$resultado = mysqli_fetch_assoc($status);
// ...
if ($resultado['online'] == 0) {

}
  • That’s right :) Thank you!

3

An alternative, just to illustrate the use of num_rows:

<?PHP
   $status = mysqli_query($connecta,
      "SELECT `online` FROM `char` WHERE `name`='[ADM] koda' AND `online` = 1");
   $resultado = mysqli_num_rows($status);

In this case, instead of knowing the user’s STATUS, just add the clause online = 1 to see if there is any record with this condition or not.

Notes:

  • I still prefer the @luciorubens answer path.

  • With MYSQLI_USE_RESULT in 2nd parameter mysqli_query the result can give problems. The ideal is MYSQLI_STORE_RESULT

  • Ah, great alternative! +1

Browser other questions tagged

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