简体   繁体   中英

How to display images from database mysql using php?

I'm stacked about displaying images from database. I have code like this:

// Get image
$image = "SELECT * FROM ".TABLE_PREFIX."mod_img WHERE section_id = '$section_id' ORDER BY number ASC";
$get_img = $database->query($image);
$img = $get_img->fetchRow();
//build images
function tampil($str){
    $arr = explode('-',trim($str));
    return $arr[0];
}
$imglist = '';
$curdir = $section_id."/";
if($img) {
    foreach($img as $im){
        $imglist .= '<div style="float:left; align="center";"><img style="margin:03px;" src="'.$baseurl.$curdir.'thumbs/'.$im['name'].'" alt="'.$baseurl.$curdir.$im['name'].'" title="'.$baseurl.$curdir.$im['number'].'"><figcaption><center>IMG '.$im['number'].'</center></figcaption></div>';
    }
}
else{
    $imglist .= '<i>No image yet</i>';  
}

But it doesn't work and there are warning :

Warning: Illegal string offset 'name' in C:\xampp\htdocs\wb\modules\pagina_toevoegen\modify.php on line 81
..................
..................

Please help me and sorry I'm new in programming.

When I didn't use foreach looping, it works, but only displaying one image.

Add while loop around your code and fix the rest of the code accordingly.

  while ($img = $get_img->fetchRow()) {

    }

in your case

$img = $get_img->fetchRow();

fetches only one row and you dont see the rest of the rcords

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM