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


nicholas maddren
nicholas maddren
12,793 Points

Displaying one part of a PDO string

Hey guys I'm having an issue trying to get my PHP to display the first url of my exploded string and non of the others.

The SQL table column contains strings like this:


I am currently using this code to display the table column onto my front end:

<img src="'.implode('"/><img src="',explode(',', $row["PictureRefs"])[0]).'"/>

However I am getting a parse error due to the fact I have added [0] to the code to try and display only the first exploded part of the string.

Any idea how I can only display the first exploded part of the string?


2 Answers

Hang on, I think I understand what you're trying to do. If you just want the first URL in that string, this should work:


$first_item = explode( ',', $sql_string )[0];

Will this work? I'm having some difficulty understanding what you're trying to do.


$srcs = explode( ',', $sql_string );

foreach ( $srcs as $src ): ?>

   <img src="<?php echo $src; ?>" />

<?php endforeach;