如何在MySQL中以单行的形式获取以逗号分隔格式存储的图像?

如何在MySQL中以单行的形式获取以逗号分隔格式存储的图像?

问题描述:

I am uploading multiple images in the database in a single column with comma separated format. I am first creating a folder for each user and inside that folder, images are stored. car_images is my root directory inside that with the name of logged_in user new directory will create.

My images are stored in below format : enter image description here

Here, emp_code is unique I am using this as SESSION and for the name of the folder of each user. as you see car_images column where I am storing image name with ,. I am using below code to stored images in folders and database. I don't know idea how to fetch all images of a single user.

$car_images_all="";
        for($i=0; $i<count($_FILES['car_images']['name']); $i++) 
        {
           $tmpFilePath = $_FILES['car_images']['tmp_name'][$i];    
           if ($tmpFilePath != "")
            {    
                if (!file_exists('car_images/'.$emp_code)) 
                    {
                        mkdir('car_images/'.$emp_code, 0777, true);
                    }
                $location = 'car_images/'.$emp_code.'/';
               $name = $_FILES['car_images']['name'][$i];
               $size = $_FILES['car_images']['size'][$i];

               list($txt, $ext) = explode(".", $name);
               $file= time().substr(str_replace(" ", "_", $txt), 0);
               $info = pathinfo($file);
               $filename = $file.".".$ext;
               if(move_uploaded_file($_FILES['car_images']['tmp_name'][$i], $location.$filename)) 
               { 
                  $car_images_all.=$filename.",";
               }
            }
        }

我在数据库中以逗号分隔格式上传多个图像。 我首先为每个用户创建一个文件夹,并在该文件夹中存储图像。 car_images code>是我的根目录,其名称为 logged_in code>用户新目录将创建。 p>

我的图片以下列格式存储: p>

此处, emp_code code>是唯一的我将其用作SESSION和每个用户的文件夹名称。 as你看到 car_images code>列,我用, code>存储图像名称。 我正在使用下面的代码将图像存储在文件夹和数据库中。 我不知道如何获取单个用户的所有图像。 p>

  $ car_images_all =“”; 
 for($ i = 0; $ i&lt; count(  $ _FILES ['car_images'] ['name']); $ i ++)
 {
 $ tmpFilePath = $ _FILES ['car_images'] ['tmp_name'] [$ i];  
 if($ tmpFilePath!=“”)
 {
 if if(!file_exists('car_images /'.$ emp_code))
 {
 mkdir('car_images /'.$ emp_code,0777,true);  
} 
 $ location ='car_images /'. $ emp_code。'/'; 
 $ name = $ _FILES ['car_images'] ['name'] [$ i]; 
 $ size = $ _FILES [  'car_images'] ['size'] [$ i]; 
 
 list($ txt,$ ext)= explode(“。”,$ name); 
 $ file = time()。substr(str_replace(  “”,“_”,$ txt),0); 
 $ info = pathinfo($ file); 
 $ filename = $ file。“。”。$ ext; 
 if(move_uploaded_file($ _ FILES ['  car_images'] ['tmp_name'] [$ i],$ location。$ filename))
 {
 $ car_images_all。= $ filename。“,”; 
} 
} 
} 
   code  >  pre> 
  div>

 <?php 
 $x=0;                                                
 $car_img =explode(",",$car_images);
 foreach($car_img as $car_images{                                                  
 $x+=1;0
 ?>                                                 
 <?php   echo '<img src="'."images/".$car_images.'" width="70" height="70" />';?>      

First of all, Select all images from database by emp_code (as you want all images of a particular user/emp).

select car_images from TableName where emp_code = $emp_code;

Now, by this you will get one or more rows. So, add one by one images to array with using explode() function per row (i.e. if row has multiple image name with comma separated). Use the array as per your choice of manipulation.

Below code will fetch the data from table and save images in array. For more details check the explode function.

$query = "select car_images from table_name where emp_code = 'emp_code'";
$result = mysqli_query($connection,$query);
$row = mysqli_fetch_assoc($result);
$images = $row['car_images'];
$images = explode(',',$images);
foreach($images AS $image){
    echo '<img src="car_images/'.$emp_code.'/'.$image.'">';
}

You can use GROUP_CONCAT() function

SELECT GROUP_CONCAT(car_images) FROM TableName

You can use group by also with emp_code or brand_id or any other column whatever your business logic is met.

SELECT GROUP_CONCAT(car_images) FROM TableName GROUP BY emp_code

OR

SELECT GROUP_CONCAT(car_images) FROM TableName GROUP BY brand_id