简体   繁体   English

如何使用PHP和MYSQL查看以BLOB格式存储的图像

[英]How to view images stored in BLOB format using PHP and MYSQL

I am doing a registration form where i am also saving image to database, well that image is being stored in LONGBLOB format. 我正在做一张注册表,我也在其中将图像保存到数据库中,以及该图像是以LONGBLOB格式存储的。 I am able to add and view all the data accept the image part. 我能够添加并查看所有接受图像部分的数据。

Here is my product_register.php code: 这是我的product_register.php代码:

<form role="form" method="post" action="product_register.php">  
  <fieldset>
    <div class="form-group">  
      <input class="form-control" placeholder="Product Name" name="product_name" type="text" autofocus>  
    </div>  
    <div class="form-group">  
      <input class="form-control" placeholder="Product Code" name="product_code" type="text" autofocus>  
    </div>
    <div class="form-group">  
      <input class="form-control" placeholder="Product Image" name="product_image" type="file" value="">  
    </div> 
    <div class="form-group">  
      <input class="form-control" placeholder="Purchase Date" name="date" type="date" autofocus>  
    </div> 
    <div class="form-group">  
      <input class="form-control" placeholder="Time Guarantee" name="time_guarantee" type="text" autofocus>  
    </div>  

    <input class="btn btn-lg btn-success btn-block" type="submit" value="submit" name="submit" >
  </fieldset>  
</form>  
<?php  
   include("db_conection.php");//make connection here  
   //mysql_close();

   if(isset($_POST['submit']))  
    {
      $usern = $_SESSION['sess_username'];
      $product_name = $_POST['product_name'];
      $product_code = $_POST['product_code'];
      $product_image = $_POS['product_image'];
      $date = $_POST['date'];
      $time_guarantee = $_POST['time_guarantee'];
      $insert_user = "insert into product (product_name,product_code,product_image,date,time_guarantee,usern) VALUE ('$product_name','$product_code','$product_image','$date','$time_guarantee','$usern')";  
      if(mysqli_query($dbcon,$insert_user))  
       // $result = mysql_query($insert_user);  
       {  
          echo "<script type='text/javascript'>alert('Product added successfully !!!')</script>";
          // echo"<script>window.open('product_register.php','_self')</script>";  
          //  mysql_close();
       }  
   }  
?>

Here is the code to view it view_product.php code: 这是查看它的代码view_product.php代码:

 <table class="table table-bordered table-hover table-striped" style="table-layout: fixed">
    <thead>    
       <tr>
  <!--   <th>User Id</th>  -->
         <th>Product Name</th>
         <th>Product Code</th>
         <th>Product Image</th>
         <th>Date of Purchase</th>
         <th>Time Guarantee</th>
    <!-- <th>Delete User</th> -->
       </tr>
     </thead>

     <?php
        mysql_connect("localhost","root","") or die(mysql_error());
        mysql_select_db("gmgmt") or die(mysql_error());

        //  $view_users_query="select * from users WHERE role='user'";     //select query for viewing users.
        //  $view_users_query="select * from users WHERE role='admin'";     //select query for viewing users.
        $view_users_query="select * from 'product' WHERE 'usern' = 'demo'";     //select query for viewing users.
        //  $run=mysql_query($view_users_query);//here run the sql query.    // '".$_SESSION['userid']."'
        $check = mysql_query("SELECT * FROM product WHERE usern = '".$_SESSION['sess_username']."' ") or die(mysql_error());

        while($row=mysql_fetch_array($check))  //while look to fetch the result and store in a array $row.
        {
           $id=$row[0];
           $product_name=$row[1];
           $product_code=$row[2];
           $product_image=$row[3];
           $date=$row[4];
           $time_guarantee=$row[5];
        // $usern=$row[6];
        ?>
        <tr>
<!--here showing results in the table -->
  <!--     <td><?php echo $id;  ?></td>  -->
           <td><?php echo $product_name;  ?></td>
           <td><?php echo $product_code;  ?></td>
           <td><?php echo "<img src='php/imgView.php?imgId=".$product_image."' />";  ?></td>
           <td><?php echo $date;  ?></td>
           <td><?php echo $time_guarantee;  ?></td>
    <!--   <td><a href="delete.php?del=<?php echo $id ?>"><button class="btn btn-danger">Delete</button></a></td> -->  
        </tr>
        <?php } ?>
    </table>

I searched a lot but I didn't find a code or method that suits my needs. 我进行了很多搜索,但没有找到适合我需要的代码或方法。

echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['imageContent'] ) . '" />';

Use the above code to display you image store in mysql database. 使用上面的代码显示您在mysql数据库中的映像存储。 Hope this will help you 希望这个能对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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