简体   繁体   English

我试图在HTML文件中通过PHP通过MYSQL显示图像。 我的代码有什么问题?

[英]I am trying to display an image from MYSQL via PHP in an HTML file. What is wrong with my code?

Here is the PHP code. 这是PHP代码。 What I'm basically trying to do is display a single image from a mySQL database, but for the life of me I can't seem to figure it out. 我基本上想做的是显示来自mySQL数据库的单个图像,但是对我而言,我似乎无法弄清楚。

    <?php
    $port = "****";
    $server = "****".$port;
    $dbname ="****";
     $user = "****";

    $conn = mysql_connect ("$server", "$user", "$pass") or die ("Connection
    Error or Bad Port");

    mysql_select_db($dbname) or die("Missing Database");

    ?>

     <!doctype html public "-//w3c//dtd html 4.0 transitional//en"
     "http://www.w3.org/TR/REC-html40/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
    />
     <title>Query 3</title>
     </head>

   <body bgcolor="white">


    <hr>


      <?php
    $speakerPic = $_POST['speakerPic'];
    $query = "SELECT speaker.speaker_picture FROM  Speaker JOIN Contact USING(contact_id) 
    WHERE Contact.lname = ";
    $query = $query."'".$speakerPic."';";
    ?>

        <p>
    The query:
    <p>
    <?php
    print $query;
    ?>

    <hr>
    <p>
     Result of query:
    </p>

     <?php
   $row = mysql_fetch_array($query);
   $content = $row['image'];

   header('Content-type: image/jpg');
         echo $content;
 ?>

 </body>
 </html>

What isn't working here? 这里什么不工作? I really do appreciate your help. 非常感谢您的帮助。

To include an image in an HTML document you use an <img> tag with a src attribute that contains a URI pointing at the image. 要将图像包含在HTML文档中,请使用<img>标记,该标记的src属性包含指向该图像的URI。

That could be an HTTP URI (which could hit a PHP script that gets the image data from a database and returns it) and it could be a DATA URI with the image encoded directly into the page. 可以是HTTP URI(可以打入PHP脚本,该脚本从数据库获取图像数据并返回),也可以是数据URI ,图像直接编码到页面中。

The usual approach to dealing with images is to keep them on a filesystem and store URLs (or file paths that URLs can be constructed from) in the database. 处理图像的常用方法是将它们保留在文件系统上,并将URL(或可以构造URL的文件路径)存储在数据库中。

You can't just spit out an HTTP response in the middle of another HTTP response as you are trying to do now. 不能像现在尝试那样在另一个HTTP响应中间吐出一个HTTP响应。

You need to call mysql_query before you call mysql_fetch_array . 您需要先调用mysql_query,然后再调用mysql_fetch_array

$result = mysql_query( $query);
$row = mysql_fetch_array($result);

Also, you should sanitize $speakerPic like so: 另外,您应该这样清理$speakerPic

$query = $query."'". mysql_real_escape_string( $speakerPic)."';";

您没有名为“图片”的行,请尝试像这样在查询中重新命名您的行:

SELECT speaker.speaker_picture AS image FROM ...

暂无
暂无

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

相关问题 PHP 安装没有 php.ini 文件。 我究竟做错了什么? - PHP installation does not have php.ini file. What am i doing wrong? 我试图从我的PHP文件中调用python脚本recommended.py。 这是正确的方法吗? 没有获得任何结果 - I am trying to call a python script recommend.py from my PHP file. Is it the correct way ? No results are obtained 我正在尝试使用PHP表单创建网页。 我的代码有什么问题? - I am trying to create a webpage with a PHP form. What is wrong about my code? 在PHP和MySQL中上传个人资料图片:我做错了什么? - Upload a Profile Image in PHP & MySQL: What am I doing wrong? 我正在尝试在 php 中创建一个搜索栏来搜索我的 csv 文件。 但是,我无法检索任何内容 - I am trying to make a search bar in php to search search my csv file. However, I am not able to retrieve anything 尝试将输入文本字段传递到.php文件以写入.txt文件时,我做错了什么? 测试代码如下: - What am I doing wrong trying to pass and capture an input text field to a .php file for writing to .txt file? Test Code follows: 我在 php 文件中的 MYSQL 代码查询有什么问题? - What's wrong with my MYSQL Code query in php file? 我正在尝试以表格格式从sql数据库显示信息,我在做什么错呢? - I am trying to display information in a table format from sql database what am I doing wrong? PHP / MYSQL我在做什么错 - PHP/MYSQL What am i doing wrong 我正在尝试在php中显示图像 - I am trying to display an image in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM