简体   繁体   English

如何使用PHP从具有其他列文本的表格中获取图像

[英]How to get images from a table with other column texts using PHP

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. 我在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);     
                        }   
                ?> 

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');

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). 通常在这种情况下,您会在表中回显一个图像标签,该图像标签链接到另一个传递ID(或其他唯一标识符)的脚本。 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 然后创建一个脚本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. 您仍然需要执行连接/查询操作,实际上,您应该使用PDO / mysqli,因为不建议使用mysql_ *函数并将其标记为删除。 that should get you a start though. 那应该可以让您开始。

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. 确实没有一种方法可以与其他数据同时获得图像数据,因为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. 在当前用于生成HTML的脚本中,您只需要让IMG标签引用SRC作为新脚本即可完成检索图像数据的工作。 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. 我假设那里$ Row [0]拥有当前记录的唯一键。 Then you write another script, display_image.php that fetches just the image data and uses the proper headers to display it: 然后,编写另一个脚本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;

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];

I hate to be the kid that gives up, but after creating the image.php 我讨厌成为放弃的孩子,但是在创建了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 然后是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. 我不知道在数据库中设置mime类型是否也很重要。 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! 保存名称而不是Blob,然后检索图像的名称,该图像将成为文件夹中的路径!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM