1

Here is my problem: I want to add small video clip to my database.

I used longblob type in MYSQL. I tried to insert small video clip to phpmyadmin but I failed to attach it.

  1. I want to know how to insert a video clip into the MYSQL database
  2. How to retrieve that video to PHP.

Please guide me to this. I searched, but I could not found a satisfying answer.

Tobias Roland
  • 1,182
  • 1
  • 13
  • 35
vish
  • 45
  • 2
  • 7

1 Answers1

1

It would probably be better practice to store the file name in the database and the video on the server itself, since video files can be quite heavy. However, that being said, I think a solution like this should work:

1. Inserting the video in the database

You need to have a table to insert the video into; Something like this

 CREATE TABLE VIDEOTABLE (
           ID INTEGER NOT NULL PRIMARY KEY,
           VIDEONAME VARCHAR (64),
           VIDEO  LONGBLOB );

would give you a table like this:

    -----------------------------------
Row | ID | VIDEONAME  | VIDEO         |
    -----------------------------------
 1  | 33 | My Movie   | 10100101...*  |
    -----------------------------------

(*I don't actually know how the video-data would be represented in the VIDEO row.)

Once you've created that table, you'd need to insert the movie into it - let's say you have your video saved on your PC at C:\users\example\video\MyMovie.mp4

INSERT INTO VIDEOTABLE VALUES (33, "My Movie", LOAD_FILE("C:\\users\\example\\video\\MyMovie.mp4"));

2. Retrieving the file from the database

When it comes to retrieving the video again, have a look at this excellent guide from about.com about retrieving the files from a databse with PHP. The entire tutorial is very helpful guide when it comes to writing files to a database, so you'll definitely want to take a look at the whole thing. Here's the code-snippet - you should be able to modify it to suit your needs if you read the guide.

<?php 
 mysql_connect("your.server.com","username","password"); 
 mysql_select_db("database_name"); 
 $query = "SELECT data,filetype FROM uploads where id=$id"; 
 $result = MYSQL_QUERY($query); 
 $data = MYSQL_RESULT($result,0,"data"); 
 $type = MYSQL_RESULT($result,0,"filetype"); 
 Header( "Content-type: $type"); 
 print $data; 
 ?> 
Tobias Roland
  • 1,182
  • 1
  • 13
  • 35