如何在php中使用blob图像获取所有数据库记录

时间:2017-10-20 15:52:16

标签: php mysql arrays json

我从mysql数据库学生信息中获取它并且在json编码中包含的数组中打印得很好,但是在我包含图像的学生记录中我在屏幕上什么都没有得到我需要的是获取带有图像的所有记录每条记录。请帮我 。 The results which come out when photo is not include in sql query

当我在sql查询中包含图片时,我的代码如下:屏幕上没有任何内容:

   public function StudentsOfParent($mobile){

 $stmt = $this->conn->prepare("SELECT
                         a.id,
                         a.name, 
                         a.mobile, 
                         c.id as sutdentId, 
                         c.user_id, 
                         c.full_name, 
                         c.school, 
                         c.level,
                         c.year,
                         c.photo,
                         d.id as busId,                   
                         d.bus_name as plate_no,
                         e.id as DriverId,                           
                         e.driverphone_number,                          
                         e.driver_fullname                          
                         from users a 
                         join students c 
                         on a.id = c.user_id
                         join buses d 
                         on c.id = d.student_id
                         join drivers e
                         on e.id = d.driver_id where a.mobile= ?");

    $stmt->bind_param("i", $mobile);
    if ($stmt->execute()) {

   $result = $stmt->get_result();

    $usersArr = array();
    while ($user = $result->fetch_assoc()){
                $usersArr[] = $user;
     }
   $stmt->close();
   // echo json_encode($usersArr);
    return $usersArr;

  } else {
return NULL;
  }
}

要访问上述功能,请使用以下php文件:

               <?php 
  include './DbHandler.php';
 $db = new DbHandler(); 
 // json response array
 $response = array("error" => FALSE);
  if (isset($_POST['mobile'])){
    $mobile = $_POST['mobile'];
   $usersArr = $db->StudentsOfParent($mobile);
     if ($usersArr != false) { 
   $response["error"]= FALSE;
   $response["user"] = $usersArr;
   echo json_encode($response); 

        }
       else {
       // user is not found with the credentials
       $response["error"] = TRUE;
       $response["error_msg"] = "Sorry we could not find you !";
         echo json_encode($response);
     }
    }
        else {
    // required post params is missing
   $response["error"] = TRUE;
   $response["error_msg"] = "Required parameter is missing!";
   echo json_encode($response);
   }
      ?>

1 个答案:

答案 0 :(得分:0)

使用此解决方案,我使用了以下函数,并将BLOB数据放入数组中 然后我将该数组包装/封装到Json_encode,最后我达到了我的目标 从MySQL显示图像BLOB。我现在需要的是在android中解码这个json数组。

我们甚至可以使用以下网址解码此图片:echo '<img src="data:photo/jpeg;base64,'.base64_encode( $user['photo'] ).'"/><br/>';

这是工作代码:

                 public function StudentsOfParent($mobile){

 $stmt = $this->conn->prepare("SELECT
                         a.id,
                         a.name, 
                         a.mobile, 
                         c.id as sutdentId, 
                         c.user_id, 
                         c.full_name, 
                         c.school, 
                         c.level,
                         c.year,
                         c.photo,
                         d.id as busId,                   
                         d.bus_name as plate_no,
                         e.id as DriverId,                           
                         e.driverphone_number,                          
                         e.driver_fullname                          
                         from users a 
                         join students c 
                         on a.id = c.user_id
                         join buses d 
                         on c.id = d.student_id
                         join drivers e
                         on e.id = d.driver_id where a.mobile= ?");

    $stmt->bind_param("i", $mobile);
    if ($stmt->execute()) {

   $result = $stmt->get_result();

    $usersArr = array();
    while ($user = $result->fetch_assoc()){

    $usersArr[] = $user['id'];
    $usersArr[] = $user['name'];
    $usersArr[] = $user['mobile'];
    $usersArr[] = $user['sutdentId'];
    $usersArr[] = $user['user_id'];
    $usersArr[] = $user['full_name'];
    $usersArr[] = $user['school'];
    $usersArr[] = $user['level'];
    $usersArr[] = $user['year'];
    $usersArr[] = base64_encode($user['photo']);   
    $usersArr[] = $user['busId'];
    $usersArr[] = $user['plate_no'];
    $usersArr[] = $user['DriverId'];
    $usersArr[] = $user['driverphone_number'];
    $usersArr[] = $user['driver_fullname'];
   // echo '<img src="data:photo/jpeg;base64,'.base64_encode( $user['photo'] ).'"/><br/>';
  }
   $stmt->close();
   // echo json_encode($usersArr);
    return $usersArr;

  } else {
return NULL;
  }
}