Juan Castro Lurita
Juan Castro Lurita

Reputation: 53

Storing an image with 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.

Upvotes: 0

Views: 1474

Answers (2)

Manish Gaikwad
Manish Gaikwad

Reputation: 348

Saving the image in database table may not be a good idea. As database size will grow heavily, instead store the files in some folder on server & store the filename(url of the file)

Upvotes: 1

Tim Sheehan
Tim Sheehan

Reputation: 4024

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.

Upvotes: 0

Related Questions