2017-10-06 53 views
-1

我使用json和php来显示数据库的内容,直到我为数据库实现了一个blob值才能存储我的图片,现在当我运行时页面它不显示数据,我使用的代码是波纹管数组在数据库中使用BLOB时不显示内容

library2页

<?php 
ini_set("display_errors",1); 
    error_reporting(E_ALL); 

function getAllPictures() { 
    // include the login credentials 
    include ("loginasdf.php") ; 
    // connect to the database to get current state 
    $conn = mysqli_connect($servername, $username, $password, $dbname); 


    if (!$conn) { die("Connection failed: " . mysqli_connect_error()); 
    echo "fail";  } 
    $sql = "SELECT * FROM Pictures" ; 
    $result = mysqli_query($conn, $sql); 
    // convert to JSON 

    $rows = array(); 
    while($r = mysqli_fetch_assoc($result)) { 
     $rows[] = $r; 
    } 
    var_dump($rows); 
    return json_encode($rows); 
    } 

显示页

<?php 
include("library2.php") ; 
$picturetxt = getAllPictures() ; 
$picturejson = json_decode($picturetxt) ;  

    $cl = $picturejson; 
    for ($i=0 ; $i<sizeof($cl) ; $i++) { 
    echo "<a href=displaycontact2.php?id=" ; 
    echo $cl[$i] -> id ; 
    echo ">" ; 
    echo $cl[$i] -> hname ; 
    echo "</a><br/>" ; 
    echo "</a><br/>" ; 
     echo "ID: "; 
      echo $cl[$i] -> ID; 
     echo "<br/>"; 
     echo "Name: "; 
      echo $cl[$i] -> hname; 
     echo "<br/>"; 
     echo "Image: "; 

     ?> 
<html>  
    <img src=himage alt="himage" style="width:304px;height:228px;"> 
</html> 
<?php 
    } 
    ?> 

I get this when i dump $rows

+1

,你会期望看到什么,当你倾倒$行?这个“blob”是图像的二进制数据,正如你所描述的那样。如果你想通过'json_encode'传输它,你可能需要'先base64_encode' blob,然后'base64_decode'它被接收后...... –

+0

我没有看到你实际尝试使用blob数据的位置在你的代码中。我甚至不会在你的帖子中看到问题。 –

+0

@cale_b问题是当我试图显示数据的页面是空的 – mattdoesnthaveaclue

回答

0

因为我怀疑问题在于你的json_encode/json_decode没有很好地处理二进制图像数据。

为了解决这个问题,你应该在分配给你的数组之前使用base64_encode,然后在json_decode之后使用base64_decode

事情是这样:

$rows = array(); 
while($r = mysqli_fetch_assoc($result)) { 
    // base64_encode the image only 
    $r['himage'] = base64_encode('himage'); 
    $rows[] = $r; 
} 

然后:

$picturejson = json_decode($picturetxt);  

// may I suggest some simpler code below... don't use for ($i=0... 
// $cl = $picturejson; 
// for ($i=0 ; $i<sizeof($cl) ; $i++) { 

// instead, use foreach($picturejson.... 
foreach ($picturejson AS $row) { 
    // base64_decode the image data 
    $row->himage = base64_decode($row->himage); 
    echo "<a href=displaycontact2.php?id=" ; 
    // access $row instead of $cl[$i].... 
    echo $row->id ; 
    // ... etc 
    echo "<img src='{$row->himage}'>"; 
    // ... etc 
} 
+0

如果你使用这个答案,你可以在这里评论,并参考答案中的代码。 –