0

我正在尝试将函数与 foreach 一起使用,但出现错误。这是我的代码:

function student_image_list(){
    global $host, $dbname, $user, $pass;
    $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $STH = $DBH->query("SELECT DISTINCT folder_name from school_students_images");
    $STH->setFetchMode(PDO::FETCH_ASSOC);
    $row = $STH->fetch();
    return $row;
}

我想从 table 打印所有唯一的文件夹名称school_students_images,我正在尝试这个:

$folder_name1 = student_image_list();
foreach($folder_name1['folder_name'] as $folder_name)  
    { 
        echo ' <div class="mws-panel grid_2">
               <div class="mws-panel-header">
               <span>'.$folder_name.'</span>
               </div>                            
               </div>';
    }

错误警告:在第 41 行的 student_image_list_raw.php 中为 foreach() 提供的参数无效

foreach($folder_name1['folder_name'] as $folder_name)  

请帮忙。

4

1 回答 1

1

你只取一行,似乎没有'folder_name'键,你必须

function student_image_list(){
    global $host, $dbname, $user, $pass;
    $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $STH = $DBH->query("SELECT DISTINCT folder_name from school_students_images");
    $STH->setFetchMode(PDO::FETCH_ASSOC);
    return $STH;
}

$STH = student_image_list();
while (($row = $STH->fetch()) !== false) {
    echo ' <div class="mws-panel grid_2">
           <div class="mws-panel-header">
           <span>'.$row['folder_name'].'</span>
           </div>                            
           </div>';

}
于 2012-05-25T11:43:25.070 回答