1

G'day I was just wondering how I could insert an image to a database. I've created a table that has already existing data for every column, and I've added an "Image" column, how would I insert an image to a particular row?

JamsHack
  • 9
  • 2
  • 10

4 Answers4

2

You do not want to do it this way. Rather, use the filesystem for what it's good for -- storing data. Have the names for the image and what they correspond to in your SQL tables.

hd1
  • 33,938
  • 5
  • 80
  • 91
0

Try this code:

CREATE TABLE Employees
(
  Id int,
  Name varchar(50) not null,
  Photo varbinary(max) not null
)


INSERT INTO Employees (Id, Name, Photo) 
  SELECT 10, 'John', BulkColumn 
  FROM Openrowset( Bulk 'C:\photo.bmp', Single_Blob) as EmployeePicture
Bruno Croys Felthes
  • 1,183
  • 8
  • 27
0

Here's the PHP way:

$fileblob =file_get_contents("path/to/imagefile");
$cleandata=addslashes($fileblob);


$sql="update mytable set blobcolumn='$cleandata' where pkcol=$mypkvalue";

mysql_query($sql);
Jimzie
  • 737
  • 5
  • 6
0

hd1 has a good point; the filesystem is better at handling image objects - and it will be faster for users to retrieve and cache the object.

However to answer it you need to store it in a blob ddatatype. (Binary large object). Remember to escape the source before importing it into your database, and un-escaping it on retrieval.

INSERT INTO table (blobcolumn) VALUES (...blob contents here...);

James Bone
  • 576
  • 1
  • 4
  • 9