问题
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');
回答1:
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.
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>";
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;
回答2:
I am assuming you are just echo'ing out the binary source of the image expecting to see a picture. This isn't the way pictures work. Usually in a case like this, in your table you would echo out an image tag that links to another script passing an id (or other unique identifier). The other script pulls the image from the database and sends the correct headers. something like:
//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]}'>";
then create a script 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");
}
you still need to do the connect/query stuff, and really you should be using PDO/mysqli because the mysql_* functions are deprecated and marked for removal. that should get you a start though.
回答3:
If you want to use array items in a string, use { and } around it, or use string concatenation:
$row = array(1,2,3);
echo "this is item1: {$row[0]}";
echo "and this is item2: ".$row[1];
回答4:
I hate to be the kid that gives up, but after creating the 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;
}
?>
and then the 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
After that I'm getting an error:
the image can not be displayed because it contains errors
I tried uploading different types. I don't know if setting the mime type in the DB would matter as well. I even Used this guys tutorial :
Part1 Part2
Now After all this! My Solution is just to move the file to somewhere on the server! Save the name instead of the Blob, then retrieve name of the image which will be it's path on the folder!
来源:https://stackoverflow.com/questions/14367872/how-to-get-images-from-a-table-with-other-column-texts-using-php