1

MySQL

CREATE TABLE document_control (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
person VARCHAR(40),
dateSent TIMESTAMP,
fileAttachment MEDIUMBLOB
);

MySQL Insert record query

INSERT INTO DOCUMENT_CONTROL (fileattachment) values (load_file('C:\Users\<user>\Desktop\test.docx'));

Retrieving record

If I run this query here: SELECT * FROM document_control - Everything is null - even after the insert query above.

Question

Why is the values null? and also.. how can I properly store a .docx file into MySQL and open the file?

NXT
  • 1,981
  • 1
  • 24
  • 30
Eduards
  • 1,734
  • 2
  • 12
  • 37
  • why didn't you go with, store file on server location and save path in MySQL and retrieve file from location when you need it? – PHP Ninja Dec 18 '19 at 11:54
  • 1
    Anyone can access the server - and delete it by accident – Eduards Dec 18 '19 at 11:55
  • Does this answer your question? [How to insert a file in MySQL database?](https://stackoverflow.com/questions/5959043/how-to-insert-a-file-in-mysql-database) – PHP Ninja Dec 18 '19 at 11:55

1 Answers1

2

You need to look into SQL blob data type

You could also read the file as bytes, convert it into a string or base64 encoding or something, and then save that as string in database.

You could also choose to save the file-reference (file path of file) to refer to it.

JaFizz
  • 328
  • 2
  • 20
  • 1
    Thanks for the answer -the link seems useful - will update you If I have accomplished anything – Eduards Dec 18 '19 at 11:59