PDO How to fetch a single row?

How do I fetch a singe row from. Right now I am using the following while loop.


    while($row = $menulisting->fetch()) {
      $menu_title = $row["menu_kaart_type_$language_abbr"];
      echo '<h1>'. $menu_title .'</h1>';
      echo "<h2>{$row['menu_item']}</h1>";
    }

But I would like to have the following outside of the loop:


      $menu_title = $row["menu_kaart_type_$language_abbr"];
      echo '<h1>'. $menu_title .'</h1>';

How would I do that?

Thank you in advance!

Qick one:


$i=0; 
while($row = $menulisting->fetch()) { 
      $i++;
      $menu_title = $row["menu_kaart_type_$language_abbr"]; 
      echo '<h1>'. $menu_title .'</h1>'; 
      echo "<h2>{$row['menu_item']}</h1>"; 
     if($i>1){exit;}   
 }  

Close. Try

$i=0;
while($row = $menulisting->fetch()) {
	if($i==0){
		$menu_title = $row["menu_kaart_type_$language_abbr"];
		echo '<h1>'. $menu_title .'</h1>';
	}
	$i++;
	echo "<h2>{$row['menu_item']}</h1>";
} 

Why do you need the while if you need a single row? :slight_smile:

<?php
$row = $menulisting->fetch();
$menu_title = $row["menu_kaart_type_$language_abbr"];
echo '<h1>'. $menu_title .'</h1>';
echo "<h2>{$row['menu_item']}</h1>"; 

Looks like he want menu TITLE as a header then list menu ITEMS

<?php
$row = $menulisting->fetch();
echo '<h1>'. $row["menu_kaart_type_$language_abbr"] .'</h1>';
foreach( $row as $record ) {
    echo "<h2>{$record['menu_item']}</h2>";
}

Wouldn’t we need fetchall() for that?

Yes, it’s true, my bad.
So, we almost get back to your solution, but into a FOREACH.

But really, when we’re talking about a menu, it would be doubtful that there would only be one category or title so all options thus far wouldn’t work. This though would add the title to an array and we check if the title is in the array.

$headings = array();
while($row = $menulisting->fetch()) {
	$menu_title = $row["menu_kaart_type_$language_abbr"];
    if(!in_array($menu_title,$headings)){
        echo '<h1>'. $menu_title .'</h1>';
		$headings[] = $menu_title; 
    }
    echo "<h2>{$row['menu_item']}</h1>";
}

Hi guys(Drummin & Vectorialpx) sorry for the late response and thank for your input. There are indeed more then on category Drummin and the suggestion from your last post is working. :tup: But the suggestion from vectorialpx from post #4 was also not far from a working version. Only instead of just fetching the single row i combined iit with a whike loop