2013-04-08 200 views
3

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

的JavaScript:

<script> 

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

     }); 
    }); 
    </script> 

PHP

(images.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>'; 

    ?> 
+0

'$ stmt-> execute(); $ stmt-> store_result(); $ stmt-> bind_result($ Foto);'不应该在绑定它们之后存储结果吗? – hjpotter92 2013-04-08 10:19:08

+0

这没有看起来适合我的图像路径..'src =“images.php?id = ”' – bipen 2013-04-08 10:20:52

+0

@bipen哦,但它是! – hjpotter92 2013-04-08 10:22:11

回答

1

解决:我需要这样的:$语句 - > 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>'; 

?>