Always equal table check

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 logical in IF . Anyone know of a solution?

    
asked by anonymous 19.03.2015 / 22:32

2 answers

3

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

You need to verify that the result of the query equals 0:

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

}
    
19.03.2015 / 22:52
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 online = 1 clause to see if there is any registry with this condition or not.

Notes:

  • I still prefer the @luciorubens response path.

  • With MYSQLI_USE_RESULT in the second parameter of mysqli_query , the result can cause problems. The ideal is MYSQLI_STORE_RESULT

19.03.2015 / 23:41