Validate the return of select PHP

3

I am making a select and before printing the form on the screen I would like to validate if the select returned lines, as I do this I tried in several ways without success.

$stmt = $dbh->prepare($fat_ped_online_vende);
$stmt->execute();

I wanted to do something like this

if(se retornar linhas):
    //entra aqui
endif;
    
asked by anonymous 20.01.2017 / 18:17

2 answers

5

You can do so

$result = $stmt->fetchAll();
if(count($result) > 0){
  ...
}
    
20.01.2017 / 18:21
2

With rowCount () of PDOStatement :

$stmt = $dbh->prepare($fat_ped_online_vende);
$stmt->execute();
if($stmt->rowCount() > 0) // verificar se a SQL trouxe linhas
{
    $result = $stmt->fetchAll(); // resultado    
}

Note: It may happen that in some banks this does not have the expected effect by using documentation : " However, this behavior is not guaranteed for all databases and should not be relied on for portable applications " in However, this behavior is not guaranteed for all databases and should not be invoked for portable applications.

>

20.01.2017 / 18:39