I want to echo the record of a ‘select min’ query. I know how to proceed to echo out the field values of a select query that retrieves multiple records, but I don’t know how to proceed when only one record is retrieved. Hope someone can acknowledge me with the best practice when only one record is retrieved. Thank you in advance. Cheers. Marc
In case of multiple records I know how to proceed:
$qry = $connexion->query('SELECT * FROM table');
$qry->setFetchMode(PDO::FETCH_ASSOC);
while($row = $qry->fetch()){
echo $row['field1'].$row['field2'];
}
But when only one ‘record’ is return what is the best practice?
$qry = $connexion->query('SELECT min(field) FROM table');
???
You can simply call
fetchoutside of a loop, since you know that there is only one row to be fetched.In cases like this where there is not only one row but also just one column, use
fetchColumnfor more convenience: