使用mysqli_fetch_row将PHP文本添加到图像

使用mysqli_fetch_row将PHP文本添加到图像

问题描述:

I have this idea:

  $query = "SELECT vidRoles FROM videoinformation";

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

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

        mysqli_free_result($result);
    }

In one (each)vidRole field in database, I have all together divided with comma Name1,Name2,Name3.

How using this code which I showed before, take each field divided each field like:
From Name1,Name2,Name3 to:
Name1
Name2
Name3
I mean separate variables.

Finally what I need using Name1,Name2,Name3 to:

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

I am not sure if I got you. Anyway if you have a single field with multiple values separated by commas then just do:

$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)
           if (!empty($v))
               printf ("<img src=\"roles/%s.jpg\">", $v);
    }

    mysqli_free_result($result);
}

Also you should consider to re-design your table such that a single row is equal to a single image