Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Integrating PHP with Databases Using Relational Tables Understanding SQL Injections

György Varga
György Varga
19,198 Points

Illegal string offset...

Hi!

My code doesn't work it says that "Warning: Illegal string offset 'category' in /home/treehouse/workspace/inc/functions.php on line 55" https://w.trhou.se/ixrs92xvqi

Can you please help me what is the problem?

Thank you!

2 Answers

Hi György,

This type of error would indicate that you're somehow passing in a string value into your get_item_html function when it's expecting an array.

This means you're likely not loading all of the database properly into your $catalog variable.

I compared your full_catalog_array() function with what was provided in the current workspace.

You have this for line 12 in function.php:

$catalog = ($results->fetch());

But it should be this:

$catalog = $results->fetchAll();

Your index page loads for me when I make that change. So it looks like your problem is that you were only fetching a single row instead of all of them.

Ray Karyshyn
Ray Karyshyn
13,443 Points

Hey, so right now you have:

<?php
function get_item_html($id,$item) {
    $output = "<li><a href='details.php?id="
        . $item["media_id"] . "'><img src='" 
        . $item["img"] . "' alt='" 
        . $item["title"] . "' />" 
        . "<p>View Details</p>"
        . "</a></li>";
    return $output;
}
?>

The error is a result of you using double quotes for both the string you are out putting and the array key name ($item["img"]).

To fix this simply use single quotes:

<?php
function get_item_html($id,$item) {
    $output = "<li><a href='details.php?id="
        . $item['media_id'] . "'><img src='" 
        . $item['img'] . "' alt='" 
        . $item['title'] . "' />" 
        . "<p>View Details</p>"
        . "</a></li>";
    return $output;
}
?>