doushai4890 2016-09-14 10:46
浏览 38
已采纳

如何直接从mysql数据库中获取图像并使用php以json格式显示它们。如何做到这一点?

I am developing an android application.For that i need to get data from my server mysql database directly.And i want to display all the images in json format using php code. Kindly some one help me pease.Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duanhuan8983 2016-09-15 16:38
    关注

    First make sure you have your images in your public_html folder and then use that url in mysql database to retrieve and then encode the obtained results in json format and now use the link to show images in your android application by using base64 conversion.

    Here is the code for u

       <?php
       $host="localhost"; //replace with database hostname 
        $username="username"; //replace with database username 
        $password="password"; //replace with database password 
        $db_name="dbname"; //replace with database name
    
         $con=mysqli_connect("localhost", "username", "password")or die("cannot     connect"); 
         mysqli_select_db($con,"dbname")or die("cannot select DB");
         $sql = "select imagepath from TABLE_NAME"; 
    
          $result = mysqli_query($con,$sql);
          $json = array();
    
    
           if(mysqli_num_rows($result)){
           while($row=mysqli_fetch_assoc($result)){
           $json['Image_Urls'][]=$row;
    
    
            }
           print_r(json_encode($json,JSON_PRETTY_PRINT));
           }
    
    
            ?>
    

    Your Mysql database should have a column to store the imagepath and that image link should point to your images in folder.

    This is the exact solution for the question you asked.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler