获取数据库数据时发出非法字符串偏移量警告

时间:2017-10-03 07:41:13

标签: php mysql

这里有什么问题:

$items = '';
$st = $db->query("select code from map where pos = 'home01' limit 1");
$res = $st->fetchColumn();  // result: 1-2-3-4
$arr = explode("-", $res);
foreach($arr as $el) {
    $st = $db->query("select fname from banners where fname = " . $el . " limit 1");
    $row = $st->fetchColumn();
    $items .= "<img class='imgr imgr2' data-fname = '" . $row['fname'] . "' src='../banners/" . $row['fname'] . ".jpg' alt='img'>\n"; // line 88
}
echo $items;

警告:
Illegal string offset 'fname' ... on line 88

1 个答案:

答案 0 :(得分:3)

fetchColumn直接返回下一列,而不是表示该行的数组:

$fname = $st->fetchColumn();
$items .= "<img class='imgr imgr2' data-fname = '${fname}' src='../banners/${fname}.jpg' alt='img'>\n";