doufei7464 2011-12-09 00:24 采纳率: 0%
浏览 43

试图从MySQL数据库中以PHP显示图像

I attempted to display the image using PHP with the following code

<?php
header('Content-type: image/png');
 $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");


    $speakerPic = $_POST['speakerPic'];
    $query = "SELECT Speaker.speaker_picture AS image FROM  Speaker JOIN Contact c USING(contact_id) 
    WHERE c.lname = '";
    $query .= $speakerPic."';";


$result = mysql_query($query,$dbname); 
$result_data = mysql_fetch_array($result, MYSQL_ASSOC);

echo $result_data['image'];   
?>

I keep on receiving this error, The image “.../query2.php” cannot be displayed because it contains errors.

Sorry to keep on bugging you guys, but can anyone tell what the problem is?

  • 写回答

1条回答 默认 最新

  • doufang8965 2011-12-09 00:58
    关注

    Not going to lie, there is a lot of bad with the OP code.

    1. You should be pulling images from the database by id, not some string
    2. You are not sanitizing the var being used in the query
    3. You are not serving a default image if one doesn't exist
    4. Also, I would suggest storing file uris in your database, not the actual image (blob). You should store a pointer to an image on your filesystem.

    Not going to clean up the code too much, just make it less bad. I'd suggest something along these lines (untested):

    // Utility.php
    class Utility
    {
        /**
         *
         * @param mixed $id is abstract, could be name or an actual id
         * @return string?
         */
        public static function getImage($id)
        {
            try {
                $port = "*";
                $server = "*:".$port;
                $dbname ="*";
                $user = "*";
    
                $conn = mysql_connect ("$server", "$user", "$pass");
    
                if (!$conn) {
                   throw new Exception("Connection Error or Bad Port");
                }
    
                if (!mysql_select_db($dbname)) {
                    throw new Exception("Missing Database");
                }
    
                $query = "SELECT Speaker.speaker_picture AS image FROM  Speaker JOIN Contact c USING(contact_id) WHERE c.lname = " . mysql_real_escape_string($id). ";";
    
                $result = mysql_query($query,$dbname); 
                $result_data = mysql_fetch_array($result, MYSQL_ASSOC);
    
                if (!isset($result_data['image'])) {
                    throw new Exception('Image not found');
                }
    
                echo $result_data['image'];
    
             } catch Exception($e) {
    
                 error_log($e->getMessage();
    
                 return file_get_contents('/path/to/some/default/image.png');
             }   
        }
    }
    
    // image.php
    require_once 'Utility.php';
    
    header('Content-type: image/png');
    ob_start();
    Utility::getImage($_POST['speakerPic']);
    $image = ob_get_flush();
    
    echo $image;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大