2011-10-19 278 views
-1

我想要的行獲取數據庫表的Forch的它呼應一個,但有以下錯誤:錯誤:()提供的foreach無效參數

$query = $this -> db -> query(' 
    SELECT 
     @rownum := @rownum + 1 rownum, 
     tour_foreign.id, 
     tour_foreign.name, 
     MIN(tour_foreign_residence.name_re) AS name_re, 
     tour_foreign.service, 
     tour_foreign.date_go, 
     tour_foreign.date_back, 
     tour_foreign.term, 
     tour_foreign.useradmin_submit, 
     tour_foreign.date_submit, 
     GROUP_CONCAT(tour_foreign_residence.name_re 
        ORDER BY tour_foreign_residence.name_re 
        SEPARATOR " " 
        ) AS name_re_all 
    FROM tour_foreign 
     INNER JOIN tour_foreign_residence 
     ON (tour_foreign.id = tour_foreign_residence.relation) 
     JOIN (SELECT @rownum := 0) r 
    WHERE tour_foreign.name LIKE "%' . $find . '%" 
     OR tour_foreign_residence.name_re LIKE "%' . $find . '%" 
    GROUP BY tour_foreign.id '); 

foreach ($query->result() as $row) { 
echo $row->name_re_all //thisoutput is: 11111 22222 33333 44444 ... 
    foreach ($row->name_re_all as $val) { 
     echo $val . '<br>'; 
    } 
} 

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: core/Loader.php(679) : eval()'d code

Line Number: 86

+3

哪條foreach語句是第86行?如果是後者,請嘗試在foreach循環之前使用explode($ row-> name_re_all,'') – Belinda

+0

此行:'foreach($ row-> name_re_all as $ val){' –

回答

1

Foreach用於迭代槽的數組,你傳遞一個字符串作爲參數

+0

我的代碼如何? –

+0

我不確定你的意思,但如果你想問如何改變代碼,所以它的作品 - @Belinda對你的帖子的評論將幫助你。你並沒有提供太多的信息,但它看起來像你想把字符串轉換成一個數組然後打印出來。沒有意義,如果只是這樣,但無論如何 - 檢查貝琳達的評論 – Nikoloff

+1

@NicoleSpears它真的*你的*代碼?或者只是你第一次看到的代碼? –

相關問題