Validate the return of select PHP

Asked

Viewed 282 times

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 have tried this in various ways without success.

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

I wanted to do something

if(se retornar linhas):
    //entra aqui
endif;
  • 2

    What is your database ?

2 answers

5

You can do it like this

$result = $stmt->fetchAll();
if(count($result) > 0){
  ...
}

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    
}

Observing: it may happen that in some banks this does not have the expected effect by text in itself documentation: "However, this behaviour is not Guaranteed for all Databases and should not be relied on for Portable Applications" in en: "However, this behavior is not guaranteed for all databases and should not be invoked for portable applications".

References:

  • Is returning zero

  • if($stmt->rowCount() == 0):

  • will depend on SQL @Maiconfernandostefenedasil, have results? which is SQL:

  • It may also be because the $stmt->rowCount() does not work in any database to count the return of a SELECT.

  • @Augusto I’ve never heard of it, anywhere, if you happen to have a reference to it ???

  • 1

    On the very link you posted in the description, http://php.net/manual/en/pdostatement.rowcount.php

  • 1

    Good @Augusto I commented asking the bank he uses, I believe there are few banks that do not have the expected result, let’s wait for what he says!

Show 2 more comments

Browser other questions tagged

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