如何使用PHP从包含其他列文本的表中获取图像 [英] How to get images from a table with other column texts using PHP
问题描述
你好同志开发者!我正在尝试从我的数据库中检索图像,以便将其包含在我创建的这个表中。我在Google上查看的所有示例都是从仅包含图像的1个表中检索图像,但在这种情况下我无法使其正常工作。
Hello There Fellow Devs! I'm trying to retrieve an image from my database to include it with this table I created. All the Examples I looked up on Google are for retrieving images from 1 table alone that contains only images, but in this case I can't get it working.
<?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);
}
?>
我试图这样做,但仍无效:
I tried to do this, it still didn't work :
$img = $Row[15];
//column 15 is the Blob the image
$img = mysql_fetch_array($QueryResult);
$content = $img['15'];
//header('Content-type: image/jpg');
推荐答案
你无法做你想做的事。您需要将逻辑分成两个脚本。实际上没有办法在与其他数据相同的传递中获取图像数据,因为IMG标记被提供的SRC不是原始数据,而是要求服务器提供图像。
You can't do what you are trying to do. You need to separate your logic into two scripts. There really isn't a way to get the image data in the same pass as your other data because the IMG tag is fed a SRC that is not raw data, but instead asks the server to serve the image.
在生成HTML的当前脚本中,您只需要让IMG标记将SRC引用为执行检索图像数据工作的新脚本。类似于:
In your current script where you generate the HTML you just need to have your IMG tag reference the SRC as a new script that does the work of retrieving your image data. Something like:
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,它只获取图像数据并使用正确的标题来显示它:
I'm assuming there that $Row[0] holds the unique key for the current record. Then you write another script, display_image.php that fetches just the image data and uses the proper headers to display it:
$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;
这篇关于如何使用PHP从包含其他列文本的表中获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!