Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6319557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:49:35+00:00 2026-05-24T15:49:35+00:00

I am trying to store an image in the DataBase, for some reason it

  • 0

I am trying to store an image in the DataBase, for some reason it doesn’t seem to work. Here’s the structure of my table.

mysql> describe ImageStore;
+---------+----------+------+-----+---------+-------+
| Field   | Type     | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| ImageId | int(11)  | NO   | PRI | NULL    |       |
| Image   | longblob | NO   |     | NULL    |       |
+---------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)

And here is my query which inserts the image or at least thats what it should:

//Store the binary image into the database
                $tmp_img = $this->image['tmp_name'];
                $sql = "INSERT INTO ImageStore(ImageId,Image)               
                VALUES('$this->image_id','file_get_contents($tmp_image)')";
                mysql_query($sql); 

If I print the value of file_get_contents($tmp_image), then there is a tons of data on the screen. But this value doesn’t get stored in the database and that is the issue that I’m facing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:49:36+00:00Added an answer on May 24, 2026 at 3:49 pm

    Problem

    $sql = "INSERT INTO ImageStore(ImageId,Image)
            VALUES('$this->image_id','file_get_contents($tmp_image)')";
    

    This creates a string in PHP named $sql. Forget about MySQL for a minute, because you’re not executing any query yet. You’re just building a string.

    The magic of PHP means that you can write a variable name — say, $this->image_id — inside the double quotes and the variable still gets magically expanded.

    This functionality, known as “variable interpolation”, does not occur for function calls. So, all you’re doing here is writing the string "file_get_contents($tmp_image)" into the database.


    Solution (1)

    So, to concatenate the result of calling file_get_contents($tmp_image), you have to jump out of the string and do things explicitly:

    $sql = "INSERT INTO ImageStore(ImageId,Image)
            VALUES('$this->image_id','" . file_get_contents($tmp_image) . "')";
    

    (You can see even just from the syntax highlighting how this has worked.)


    Solution (2)

    Now the problem you have is that if the binary data contains any ', your query is not valid. So you should run it through mysql_escape_string to sanitize it for the query operation:

    $sql = "INSERT INTO ImageStore(ImageId,Image)
            VALUES('$this->image_id','" . mysql_escape_string(file_get_contents($tmp_image)) . "')";
    

    Solution (3)

    Now you have a really big string, and your database is getting bulky.

    Prefer not storing images in databases, where you can help it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to store image data (screenshots) in SQLite database. now = int(math.floor(time.time())) ba
I am trying to store an image uploaded by the user into the database
I am trying to store some pictures into my SQL Server database from a
I m trying to store and retrieve image and text from sqlite database..My saveDatabase
I'm trying to store the names of some variables inside strings. For example: Dim
I'm trying to store a lightly filtered copy of a database for offline reference,
I am trying to store a PNG image in a core data store backed
i am trying to load images from the northwind database (categories table, images that
I'm trying to pass some representation of an image back and forth between Silverlight
I'm trying to store images in a sql server database. I've got a column

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.