这是显示图像和消息的代码。 它以二进制显示图像,不显示消息。 此外,我无法将从我的Android应用程序发送的图像存储到数据库。
<?php
error_reporting(E_ALL);
ini_set("display errors",1);
$servername = "localhost";
$username = "root";
$password = "raspberrypi3";
$dbname = "board2";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$DefaultId =1;
$div=$_POST["div"];
$message=$_POST["data"];
$image = $_POST['image'];
// $GetOldIdSQL ="SELECT id FROM userinfo ORDER BY id ASC";
$ImagePath ="C:\\wamp\\www\\project3\\image\\$DefaultId.png";
$ServerURL = "http://www.localhost.com//$ImagePath";
//$ServerURL ="$DefaultId.png";
if($div=="2"){
$InsertSQL = "UPDATE userinfo SET `image` = '$ServerURL' WHERE `div` = $div";
}else{
$InsertSQL = "UPDATE userinfo SET `message` = '$message' WHERE `div` = $div";
}
// $InsertSQL = "INSERT INTO userinfo (`message`,`div`,`image`) VALUES('$message','$div','$ServerURL')";
if(mysqli_query($conn, $InsertSQL)){
if($div=="2"){
file_put_contents($ImagePath,base64_decode($image));
}
echo "Process done.";
}
mysqli_close($conn);
}else{
echo "Please Try Again"; }
?>
这是从Android应用程序接收代码并存储在数据库中的代码。
<?php
error_reporting(E_ALL);
ini_set("display errors",1);
?>
<html>
<head><h1><b><center>DIGITAL NOTICE BOARD</center></b></h1><br/><br/></br>
<script type="text/JavaScript">
<!--
function AutoRefresh( t ) {
setTimeout("location.reload(true);", t);
}
//-->
</script>
<body onload="JavaScript:AutoRefresh(5000);" style="background-image:url(11.jpg) ;background-size: 1600px 1200px;box-shadow: inset 0 0 0 0px black;
background-color: rgb(224, 141, 169);"height="1600" width="1200">
<?php
$servername = "localhost";
$username = "root";
$password = "raspberrypi3";
$dbname = "board2";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql1 = "SELECT * from userinfo where `div`=1" ;
$result1 = mysqli_query($conn, $sql1) or die(mysqli_error($conn)) ;
if ($rows1 = mysqli_fetch_array($result1))
{
?>
<div id="1";
style="
width: 66%;
height: 379px;
border: aqua:82;
box-shadow: 0px 0px 5px #000;
position : absolute ;
background-image: url(download.png);
">
<h1><b><font style="font-family: cursive;"><?php echo $rows1['1']
?></h1></b></font>
</div>
<?php
}
$sql2 = "SELECT * from userinfo where `div`=2" ;
$result2 = mysqli_query($conn, $sql2) or die(mysqli_error($conn)) ;
if ($rows2 = mysqli_fetch_array($result2))
{
?>
<div style="
width: 31%;
height: 380px;
border: aqua:82;
box-shadow: 0px 0px 5px #000;
position : absolute ;
left : 68%;
">
<img src="image/1.png " height="380" width="425"/>
</div>
<?php
}
$sql3 = "SELECT * from userinfo where `div`=3" ;
$result3 = mysqli_query($conn, $sql3)or die(mysqli_error($conn)) ;
if ($rows3 = mysqli_fetch_array($result3))
{
?>
<div style="
width: 97%;height: 73px;
border: aqua:82;
box-shadow: 0px 0px 5px #000;
position : absolute ;
left : 1% ;
top : 85% ;
background-image: url(maroon.png);
">
<marquee><h1><b><font style="font-family: cursive;" font color="white"><?php echo $rows3['1']
?></marquee></h1></b></font>
</div>
<?php
}
$conn->close();
//echo "Hello,Android__";
//$msg=urldecode($_POST['POST']);
//echo $msg;
?>
答案 0 :(得分:0)
如果您的图片是Blob(我不知道),您可以这样做:
<?php
$connect = mysqli_connect("localhost", "user", "pass", "db");
$query = "SELECT * FROM table";
if(mysqli_num_rows($image) > 0) {
while($row = mysqli_fetch_array($image)) {
echo
'<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'" alt="image" class="any class">';
echo
''$row['message'] ).'';
}} else { echo 'Error on connection';}?>
如果这是您所需要的,现在要存储您需要的图像:
$file = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
$query = "INSERT INTO db_table (image) VALUES ('".$file."')"; // query to insert
这应该可以解决你的问题