How to Get Record Count from Recordset (v2021)

Tips submitted by PHPMaker users
Post Reply
mobhar
User
Posts: 11703

How to Get Record Count from Recordset (v2021)

Post by mobhar »

As we've already known from "Migrating to v2021" https://phpmaker.dev/doc/migrate2021.htm we can get the recordset and return the value of a field by using the following code:

$stmt = $conn->executeQuery($sql);
while ($row = $stmt->fetch()) {
$value = $row["fieldname"];
}

Sometimes, we need to check the condition when it does not have any record from the recordset, then display the information message that there are no record found.

So, here is the code to check whether the record count is greater than zero, then display the record(s), otherwise display the message no record found.

$sql = "SELECT fieldname FROM thetable WHERE blablabla"; // define your SQL
$stmt = $conn->executeQuery($sql); // execute the query
$value = ""; // initial value
if ($stmt->rowCount() > 0) { // check condition: if record count is greater than 0
while ($row = $stmt->fetch()) { // loop
$value .= $row["fieldname"] . "<br>"; // in case the result returns more than one record, display it and separated by line break
} // end loop
echo "The result: " . $value; // display the result
} else { // if there are no result
echo "No record found."; // display the message
} // end of check condition

Happy coding, everyone!

Post Reply