douse8732 2015-10-13 02:44
浏览 43

使用PHP PDO MySQL存储图像

Hello friends of the community. I'm storing information in my MySQL database using this code:

class crud
    {
        private $db;

        function __construct($DB_con)
        {
            $this->db = $DB_con;
        }

        public function create($fname,$lname,$email,$contact,$nimagen)
        {
            try
            {
                $stmt = $this->db->prepare("INSERT INTO tbl_users(first_name,last_name,email_id,contact_no) VALUES(:fname, :lname, :email, :contact)");
                $stmt->bindparam(":fname",$fname);
                $stmt->bindparam(":lname",$lname);
                $stmt->bindparam(":email",$email);
                $stmt->bindparam(":contact",$contact);
                $stmt->execute();
                return true;
            }
            catch(PDOException $e)
            {
                echo $e->getMessage();  
                return false;
            }

        }   

}

Now I want to add a picture, Please I need to add in the code? I'm using PDO.

  • 写回答

2条回答 默认 最新

  • donglu2008 2015-10-13 02:57
    关注

    You need to store your image as a blob.

    $file = fopen('path/to/image.jpg','rb');
    
    $stmt->bindparam(":image", $file, PDO::PARAM_LOB);
    

    Edit: Since others have pointed this out I thought I'd just say it's probably best to avoid doing this and store your image in the filesystem, instead saving the filename in the database so you can reference your image that way. But this should answer your question regardless.

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助