[英]How to insert images into the database and fetch it from the database and display using PHP?
[英]How to fetch images from mysql database directly and display them in json format using php.How to do this?
我正在開發一個android應用程序。為此,我需要直接從服務器mysql數據庫中獲取數據。並且我想使用php代碼以json格式顯示所有圖像。 請有人幫助我。謝謝。
首先,請確保您的圖片位於public_html文件夾中,然后在mysql數據庫中使用該URL進行檢索,然后以json格式對獲取的結果進行編碼,然后使用該鏈接通過base64轉換在您的android應用程序中顯示圖片。
這是你的代碼
<?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));
}
?>
您的Mysql數據庫應有一列存儲圖像路徑,並且該圖像鏈接應指向文件夾中的圖像。
這是您提出的問題的確切解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.