【问题标题】:how to show the image using php如何使用php显示图像
【发布时间】:2016-04-18 13:54:40
【问题描述】:

图像已存储在数据库中。我制作了一个 php 页面,以便查看存储在我的 ms sql 数据库中的所有数据。它只显示图像的文件名而不是实际图像

<?php
$serverName = "kwekwe\SQLEXPRESS";
$connectionInfo = array( "Database"=>"customerdb", "UID"=>"dbadmin", "PWD"=>"kwe[enter image description here][1]" );
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
     die( print_r( sqlsrv_errors(), true));
}
 //declare the SQL statement that will query the database
     $query = "SELECT * FROM Customer_Details";


 //execute the SQL query and return records
     $result = sqlsrv_query($conn, $query)
         or die( print_r( sqlsrv_errors(), true));

 //Show results in table

 $o = '<table border=1 id="myTable">
         <thead>
         <tr>
         <th>Customer ID</th>
         <th>Customer Name</th>
		 <th>Image</th>
         </tr>
         </thead><tbody>';

      while ( $record = sqlsrv_fetch_array($result) )
          {
              $o .= '<tr><td>'.$record ['Cust_ID'].'</td>';
			  $o .='<td>'.$record ['Cust_Name'].'</td>';
			  $o .='<td><img height=127 width=127 src=data:image;base64>'.$record ['image'].'</td>';
			  $o .='</tr>';
          }               

       $o .= '</tbody></table>';

       echo $o;
    //free result set memory
        //mssql_free_result($result);

    //close the connection
        //sqlsrv_close($dbhandle);
    ?>

【问题讨论】:

  • 图片是BLOB的类型吗?还是只是图片的路径?
  • 如果我没弄错的话,你可以在 img 标签的 src 属性中回显文件名,而不是像 &lt;td&gt; 元素一样显示:$o .='&lt;td&gt;&lt;img height=127 width=127 src="'.$record ['image'].'"&gt;&lt;/td&gt;';

标签: php sql-server database html


【解决方案1】:

你可以这样显示:

$o .='<td><img height=127 width=127 src="'.$record ['image'].'"></td>';

【讨论】:

    【解决方案2】:

    尝试代替

    $o .='<td><img height=127 width=127 src=data:image;base64>'.$record ['image'].'</td>';
    

    这个:

    $o .='<td><img height=127 width=127 src="data:image/png;base64,' . $record['image'] . '"></td>';
    

    如果$record['image'] 中包含图像的base64 代码,这应该可以工作。

    您的图像当前未显示的原因是&lt;img&gt; 标签实际上没有指定来源。如果$record['image'] 是一个文件名,那么只需生成一个具有该文件名的 URL 并将其放在 src='' 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      相关资源
      最近更新 更多