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 c51单片机控制步进电机
  • ¥20 Visual studio无法检测到设备
  • ¥15 为什么我通过html绘制的SVG折线图插入到word中坐标轴不显示出来
  • ¥30 vue 页面窗口放大或者缩小元素会变化
  • ¥15 questasim仿真报错
  • ¥15 寻找电脑攻防的导师,有问题请教一下。
  • ¥20 微信同是win11,我的电脑安装不了pageoffice,一直无法打开
  • ¥15 这个界面我通过postman请求不到,但是通过浏览器可以正常访问
  • ¥15 多目标优化算法在与其他算法数据对比结果判断
  • ¥15 CPTN和EAST,主干网络是VGG16,请问在ICDAR2015数据集上训练之后,CPTN和EAST模型的大小为多少