且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何仅在PHP中获取第一行的特定列数据SQL?

更新时间:2022-12-10 07:47:09

Use simple flag $is_first:

Use simple flag $is_first:

$is_first = true;
foreach ($stmt as $row){
    echo $row['a'] . $row['b']  . $row['c'];

    //IF FIRST ROW, THEN ECHO $row['d'] and $row['e']
    if ($is_first) {
       echo $row['d'] . $row['e']; 
       $is_first = false;
    }
} //END FOREACH LOOP

否则你可以使用 foreach 之类的

Else you can use foreach like

foreach ($stmt as $key => $row) {
    echo $row['a'] . $row['b']  . $row['c'];
    if ($key == 0) {
        echo $row['d'] . $row['e']; 
    }
}

如果您 $stmt 是零索引数组,请检查是否 $key == 0.

and if you $stmt is zero-indexed array check if $key == 0.