When selecting and displaying records using php's
PDO
and MySql
, how can I print one row once and loop through the rest? In the code below, I need to print $row['parentName']
only once, and loop through all the other row. What is an efficient way to do it? The way I'm doing it does not work and I get Notice: Undefined variable: row in C:\web\apache\htdocs\index.php on line 227
.
I don't mind taking a whole new approach, since this isn't implemented yet.
$stmt = $conn->prepare($sql);
$stmt->execute($thisArray);
$rows = $stmt->rowCount();
if($rows >= 1) {
echo '<div>'.$row['parentName'].'</div>'; //Echo only once.
while($row = $stmt->fetch()) {
echo '<div>'.$row['childName'].'</div>';
}
} else {
echo 'We could not find anything to display.';
}
EDIT
I'm stuck at the fetch
part.