dswsl2016
2018-10-22 08:44
浏览 119
已采纳

如何在数据库MYSQLi中显示php中的图像?

I want to display images in php from my database using Mysqli. THis is my code in question.php:

    <form class="form-horizontal" role="form" id='login' method="post" action="result.php">

                <?php
                $row = mysqli_query( $conn, "select id,question_name,image from questions where category_id=1 and level_id=1 ORDER BY RAND() LIMIT 10");

                $i = 0;
                $j = 1; $k = 1;
                ?>
                <?php while ( $result = mysqli_fetch_assoc($row) ) {
                     //if ( $i == 0) echo "<div class='cont' id='question_splitter_$j'>";?>
                    <div id='question<?php echo $k;?>' >
                    <p class='questions' id="qname<?php echo $j;?>"> <?php echo $k?>.<?php echo $result['question_name'];?></p>

                     <?php echo "<img src='".$result['image']."'/>";?>
                     <input type="text" name="reponse"   required />
                    <br/>

                    </div>
                    <?php

                          $k++;
                     } ?>   
            </form>

My table is : enter image description here

But the result is like here: enter image description here

can you please help me to display my images correctly?

图片转代码服务由CSDN问答提供 功能建议

我想使用Mysqli从我的数据库中显示php中的图像。 THis是我在question.php中的代码:< / p>

 &lt; form class =“form-horizo​​ntal”role =“form”id ='login'method =“post”action =“result.php”&gt; 
 \  n&lt;?php 
 $ row = mysqli_query($ conn,“select id,question_name,category from category_id = 1和level_id = 1 ORDER BY RAND()LIMIT 10”); 
 
 $ i = 0  ; 
 $ j = 1;  $ k = 1; 
?&gt; 
&lt;?php while($ result = mysqli_fetch_assoc($ row)){
 // if if($ i == 0)echo“&lt; div class ='cont'  id ='question_splitter_ $ j'&gt;“;?&gt; 
&lt; div id ='question&lt;?php echo $ k;?&gt;'  &gt; 
&lt; p class ='questions'id =“qname&lt;?php echo $ j;?&gt;”&gt;  &lt;?php echo $ k?&gt;。&lt;?php echo $ result ['question_name'];?&gt;&lt; / p&gt; 
 
&lt;?php echo“&lt; img src ='”。  $ result ['image']。“'/&gt;”;?&gt; 
&lt; input type =“text”name =“reponse”required /&gt; 
&lt; br /&gt; 
 
&lt;  ; / div&gt; 
&lt;?php 
 
 $ k ++; 
}?&gt;  
&lt; / form&gt; 
   
 
 

我的表格是:

但结果如下:

您可以帮我正确显示我的图像吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyakui8675 2018-10-22 08:46
    已采纳

    You can do this by base64_encode()ing the binary data to the browser using the data-uri scheme:

    <?php echo '<img src="data:image/png;base64,'.base64_encode($result['image']).'" />'; ?>
    

    Alternatively, you can create an image.php script, that takes an ID, and outputs the binary data to the browser directly:

    $query = mysqli_query("SELECT * FROM ..."); //**Bind** $_GET['ID'] to the query:
    $row = mysqli_fetch_assoc($query);
    header('Content-type: image/png');
    echo $row['image'];
    

    And in your previous code, access it like:

    <?php echo '<img src="image.php?id=', $row['id'], '" />'; ?>
    
    打赏 评论