Hello With Fellow Devs!我正在尝试从我的数据库中检索图像,以便将其包含在我创建的这个表中。我在Google上查看的所有示例都是用于从仅包含图像的1个表中检索图像,但在这种情况下,我无法使其正常工作。
<?php
$Con = mysql_connect('localhost','root','');
if($Con === false)
{
echo "Not Connected";
}
else
{
$select = mysql_select_db("symfony");
$TableName = "main";
$SQLstring = "SELECT * FROM $TableName ";
$QueryResult = mysql_query($SQLstring);
$Row = mysql_fetch_row($QueryResult);
do {
echo "<div class='art-content-layout'>";
echo "<div class='art-content-layout-row'>";
echo "<div class='art-layout-cell' style='width: 33%'>";
echo" <p><img width='259' height='194' class='art-lightbox' name='image' src='../images/3.jpg'><br></p>";
echo "</div>";
echo "<div class='art-layout-cell' style='width: 67%'>";
echo "<p></p>";
echo "<table border>";
echo "<tbody>";
echo "<tr>";
echo "<tr>";
echo "<th colspan='3' align='left'><b> Owner : $Row[0]</b></th>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><b>$Row[1]:</b>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td><b>Price:$Row[9] US Dollar </b></td>";
echo "</tr>";
echo "<tr>";
echo "<td><b> City: $Row[4] </br> Hood: $Row[4] </br> Qdr: $Row[5] </br> Street:$Row[6] </br> Property Number :$Row[7] </br> Room Number : $Row[8] </b></td>";
echo" <td><b>Description : $Row[10] </b></td>";
echo "</tr>";
echo"<tr>";
echo" <td><b>Type : $Row[12] </b></td>";
echo "<td><b>Contact : $Row[1] </b></td>";
echo "</tr>";
echo "</tr>";
echo "</tbody>";
echo "</table> <br><p></p>";
echo "</div>";
echo "</div>";
echo "</div>";
$Row = mysql_fetch_row($QueryResult);
} while ($Row);
}
?>
我试图这样做,它仍然无效:
$img = $Row[15];
//column 15 is the Blob the image
$img = mysql_fetch_array($QueryResult);
$content = $img['15'];
//header('Content-type: image/jpg');
答案 0 :(得分:2)
我假设你只是回想出想要看到图片的图像的二进制来源。这不是图片的工作方式。通常在这种情况下,在您的表中,您将回显出一个图像标记,该标记链接到另一个传递id(或其他唯一标识符)的脚本。另一个脚本从数据库中提取图像并发送正确的标头。类似的东西:
//in the table where you want to show the image
//assuming id is in column 0, change to whatever
echo "<img src='image.php?id={$Row[0]}'>";
然后创建一个脚本image.php
//send out image header
header('Content-type: image/jpg');
//get the id from the url
$id = isset($_GET['id'])?(int)$_GET['id']:0;
if($id > 0){
//query database for image blob
$sql = "SELECT `imageData` FROM `table` WHERE `id`={$id}";
if($numRows){
//echo out the blob data
echo $Row[0];
} else {
//no row found in database, echo default image
readfile("/path/to/noImage.jpg");
}
} else {
//invalid id passed, echo default image
readfile("/path/to/noImage.jpg");
}
你仍然需要做连接/查询的东西,实际上你应该使用PDO / mysqli,因为不推荐使用mysql_ *函数并标记为删除。那应该可以帮到你。
答案 1 :(得分:2)
你无法做你想做的事。您需要将逻辑分成两个脚本。实际上没有办法在与其他数据相同的传递中获取图像数据,因为IMG标记被提供的SRC不是原始数据,而是要求服务器提供图像。
在生成HTML的当前脚本中,您只需要让IMG标记将SRC引用为执行检索图像数据工作的新脚本。类似的东西:
echo" <p><img width='259' height='194' class='art-lightbox' name='image' src='display_image.php?id=" . $Row[0] . "'><br></p>";
我假设$ Row [0]持有当前记录的唯一键。然后你编写另一个脚本,display_image.php,它只获取图像数据并使用正确的标题来显示它:
$currentId = $_REQUEST['id'];
// Your query code would be here using the $currentId to just retrieve the desired record
$SQLstring = "SELECT your_image_column_name FROM $TableName WHERE id = $currentId";
$QueryResult = mysql_query($SQLstring);
$img = mysql_fetch_array($QueryResult);
$content = $img['your_image_column_name'];
header('Content-type: image/jpg');
echo $content;
答案 2 :(得分:1)
如果要在字符串中使用数组项,请在其周围使用{
和}
,或使用字符串连接:
$row = array(1,2,3);
echo "this is item1: {$row[0]}";
echo "and this is item2: ".$row[1];
答案 3 :(得分:0)
我讨厌成为放弃的孩子,但在创建了image.php之后
<?php
$Con = mysql_connect('localhost','root','');
if($Con === false)
{
echo "Not Connected";
}
else
{
$select = mysql_select_db("symfony");
$id = $_REQUEST['id'];
$query = mysql_query("SELECT image1 FROM main WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image1'];
header('Content-type: image/jpg');
echo $content;
}
?>
然后是html
echo" <p><img width='259' height='194' class='art-lightbox' name='image' src='image.php?id=".$Row[16]."'><br></p>";
// where $row[16] is the image Column
之后我收到了一个错误:
无法显示图像,因为它包含错误
我尝试上传不同的类型。我不知道在DB中设置mime类型是否也很重要。我甚至使用过这个人教程:
现在这一切都结束了!我的解决方案只是将文件移动到服务器上的某个位置!保存名称而不是Blob,然后检索图像的名称,这将是文件夹中的路径!