php - 无法从数据库加载图像

标签 php jquery mysql

我正在尝试加载存储在数据库中的图像,但无法正常工作。我只能看到链接,但看不到图像。我在存储图片的字段中使用 longblob。

JavaScript:

  <script>

    $(document).ready(function(){
         $.ajax({type: "POST",
                 url: "cargaImg.php",
                 success:function(data) {
                    $('#pinta').html(data);
                    }

         });  
    });
  </script> 

PHP

(图片.php)

 <? require('conecta.php');
   $stmt=$oConni->prepare("SELECT PIC FROM FOTOS WHERE ID_PIC=?");
   $stmt->bind_param('i',$_GET['id']);
   $stmt->execute();
   $stmt->store_result();
   $stmt->bind_result($Foto);
   while ($stmt->fetch()) {
     header('Content-Type: image/jpeg');
     print $Foto;
   }
 ?>

(cargaImg.php)

    <?php
        require('conecta.php');
        ini_set('display_errors',1); error_reporting(E_ALL);  

        $cSQL="SELECT ID_PIC, PIC, NOMBRE FROM FOTOS";

        $stmt=$oConni->prepare($cSQL) or die($oConni->error);
        //$stmt->bind_param('i',$_POST['local']);
        $stmt->execute();                               
        $stmt->bind_result($id, $pic, $nombre);
        //$i=0;
        echo '<table cellspacing="0">';
        while ($stmt->fetch()) {

            if (!empty($pic)){ ?>
            <tr><td><img class="sifoto" src="images.php?id=<?=$id?>" width="60" height="60"  /></td></tr>
            <?}
            echo'<tr><td value="'.$id.'"><a target="_blank" href="'.$nombre.'">Enlace</a></td></tr>'; 
            //$i++;
        }   
        $stmt->close();
        echo'</table>';

     ?>

最佳答案

已解决:我需要这个:$stmt->store_result();

    <?php
        require('conecta.php');
        ini_set('display_errors',1); error_reporting(E_ALL);  
        $cSQL="SELECT ID_PIC, PIC, NOMBRE FROM FOTOS";
        $stmt=$oConni->prepare($cSQL) or die($oConni->error);
        //$stmt->bind_param('i',$_POST['local']);
        $stmt->execute(); 
        $stmt->store_result();
        $stmt->bind_result($id, $pic, $nombre);
        //$i=0;
        echo '<table cellspacing="0">';
        while ($stmt->fetch()) {

            if (!empty($pic)){ 
            echo'<tr><td><img class="sifoto" src="images.php? id='.$id.'" width="100" height="100"  /></td></tr>';
            }
            echo'<tr><td value="'.$id.'"><a target="_blank" href="'.$nombre.'">Enlace</a></td></tr>'; 
            //$i++;
        }   
        $stmt->close();
        echo'</table>';

?>

关于php - 无法从数据库加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15876566/

相关文章:

php - 如何从两个没有任何连接的不同表中获取值

php - 转置返回的 JSON 对象(PHP、mySQL)

Php数据库-内容加密/解密

php - 使用 PHP 匹配 Javascript 时间戳

javascript - 防止除弹出窗口及其内容外的所有元素滚动

javascript - 添加对象数组中包含的值

javascript - 广告横幅在浏览器 session 中只显示一次

mysql从一张表更新另一张表

mysql - 从另一个表 MySQL 更新地理信息

php - MySQL 只插入两个字段