wawanopoulos wawanopoulos - 6 months ago 8
SQL Question

check result from prepared statements PDO?

I use this code to get rows from database.

// Prepare WC statement
$queryUP = $pdo->prepare("SELECT * FROM unitprices WHERE id_quot = :idQuotation");
// Execute Unit prices statement
$queryUP->execute(array(
'idQuotation' => $idQuotation
));

// How to check the results is empty or not ?
if (results) {
// foreach($queryUP as $rowup) {
//...
// }
} else {
// do another thing
}


I don't how to do to check if there is some results in the query before continuing the code ?

Answer
$queryUP = $pdo->prepare("SELECT * FROM unitprices WHERE id_quot = ?");
$queryUP->execute(array($idQuotation));

//here you go
$results = $queryUP->fetchAll();

if ($results) { 
   // foreach($results as $rowup) {
      //...
   // }
} else {
   // do another thing
}

Hope you are doing your foreach in the template, not right in place as shown here.

Comments