rowCount does not return zero

0

I have this script:

<?php
include 'config.php';
if(isset($_POST)){
$user = isset($_POST['user']) ? strip_tags($_POST['user']) : 'HabboColorFS';
$rank = $pdo->query("SELECT * FROM topicos_comentarios WHERE autor='".$user."'")->rowCount();
$usuario = $pdo->prepare("SELECT * FROM usuarios WHERE usuario='".$user."'");
$usuario->execute();
    while($ver = $usuario->fetch(PDO::FETCH_ASSOC)){
?>
<?php print $usuario->rowCount(); ?>
<div class="image" style="background-image: url('<?php echo $cx_uploads; ?>/<?php echo $ver['avatar']; ?>')">
<div class="comments"><i class="fa fa-comments" aria-hidden="true"></i>&nbsp;<?php echo $rank; ?></div>
<div class="comments"><i class="fa fa-commenting" aria-hidden="true"></i>&nbsp;123</div>
<div class="base">
    <div class="avatar" style="background-image: url('https://www.habbo.com.br/habbo-imaging/avatarimage?&user=<?php echo $ver['usuario']; ?>&action=std&direction=3&head_direction=3&img_format=png&gesture=std&headonly=0&size=s')"></div>
</div>
</div>
<div class="type day">
<div class="totype"><i class="fa fa-trophy" aria-hidden="true">
</i>&nbsp;&nbsp;RANKING DO DIA</div>
<div class="position">178º</div>
</div>
<div class="type all">
<div class="totype"><i class="fa fa-trophy" aria-hidden="true">
</i>&nbsp;&nbsp;RANKING GERAL</div>
<div class="position">178º</div>
</div>
<?php } } ?>

I'm trying to make an if that when $ user-> rowCount () is 0 it will display an error message "echo" no user found. "

I put print $ user-> rowCount (); and I tried with echo tbm, I already did the if that way, but when I make a deliberate search to empty it it does not display 0.

How can I resolve?

    
asked by anonymous 27.05.2017 / 03:06

1 answer

1

It will not appear on the screen that there are zeros records in the database because the display statement of this value:

print $usuario->rowCount();

You're inside a loop:

while($ver = $usuario->fetch(PDO::FETCH_ASSOC)) {...}

Looking at the documentation for the fetch method, there is the following return description:

  

The return value of this function depends on the fetch type. In all cases, FALSE is returned on failure.

The value returned will depend on the parameter set, if successful, or FALSE on any other case. Being the number of null records, the method returns false and therefore, the loop will not execute. If you want to even display the number of records, even if it's zero, just put print out of the loop:

print $usuario->rowCount();

while($ver = $usuario->fetch(PDO::FETCH_ASSOC)) {
    ...
}
    
01.06.2017 / 14:04