使用mysqli_fetch_row将数据库TEXT用于IMAGE

I have this code:

$query = "SELECT vidRoles FROM videoinformation";

if ($result = mysqli_query($con, $query)) {

    while ($row = mysqli_fetch_row($result)) {
        $values = explode(',',$row[0]);
        foreach($values as $v)
           printf ("<img src=\"roles/%s.jpg\">", $v);
    }

    mysqli_free_result($result);
}

BUT instead of display:

<img src="roles/Name1.jpg>
<img src="roles/Name2.jpg>
<img src="roles/Name3.jpg>

I have:

<img src="roles/Name1.jpg>
<img src="roles/Name2.jpg>
<img src="roles/Name3.jpg>
<img src="roles/.jpg>
<img src="roles/.jpg>
<img src="roles/.jpg>
<img src="roles/.jpg>

But inside vidRoles filed I have only "Name1,Name2,Name3."
Why this empty images are show up???

try this -

foreach($values as $v) {
    if (!empty($v)) {
       printf ("<img src=\"roles/%s.jpg\">", $v);
    }
}

there are some null values present, that's why it is printing them.

change your print statement with

    printf ('<img src=\"roles/%s.jpg\">', $v);

Please update your query to:

$query = "
    SELECT  vidRoles 
    FROM    videoinformation 
    WHERE   LENGTH(vidRoles) > 0
";

There are some rows where vidRoles is either blank or NULL.

They are causing the problem.

So, to avoid the problem, we can either do it by PHP by if else statements, or

Simply add some SQL conditions.

I think, second one is better as it has less efforts.