1

What is wrong here:

$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;

Warning:
Illegal string offset 'fname' ... on line 88

3
  • 3
    $row = $st->fetchColumn(); I expect it should return array("a", "b", "c") not array('fname' => "a"); Commented Oct 3, 2017 at 7:43
  • var_dump($row) Like @degr said, $row['fname'] does not exist ! Commented Oct 3, 2017 at 7:44
  • Instead of $row['fname'] just use $row because fetchColumn doesn't return array Commented Oct 3, 2017 at 7:45

1 Answer 1

3

fetchColumn returns the next column directly, not an array that represents the row:

$fname = $st->fetchColumn();
$items .= "<img class='imgr imgr2' data-fname = '${fname}' src='../banners/${fname}.jpg' alt='img'>\n";
Sign up to request clarification or add additional context in comments.

2 Comments

not sure that you are right, because fetchColumn should return column, not cell. If you have limit 1 it should return array with one element, so, proper way to use it will be reset($fname) or $fname[0]
@degr this is the right way to use fetchColumn - check out the examples in the documentation page I linked to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.