user2999924
user2999924

Reputation: 3

How to play a video in a database BLOB using php?

I have managed to upload a video (<2MB) to my table as a LONGBLOB type. The table name is test2 in database 'test'. But i cant play an mp4 video. This is my code.

<?php
echo 'h';
$arr=array();
$con=mysql_connect('localhost','root','46');
mysql_select_db('test',$con);
$query="select video from test2";
$result=mysql_query($query,$con);
$arr=mysql_fetch_array($result);
header('content-type: video/mp4'); 
echo $arr[0];
?>

Thanks.

Upvotes: 0

Views: 9534

Answers (3)

Speedz
Speedz

Reputation: 151

I stumbled on this question in my search for similar. If you want to not use header and play in a div or other html container from a sql blob do this:

function displayvideo(){
    global $db_user, $db_password, $media_db_name, $db_host;
    $video = "some_video.mp4";
    $dbconnect = 'mysql:dbname='.$media_db_name.';host='.$db_host.'';
    try {
        $db = new PDO($dbconnect, $db_user, $db_password);
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
    if (($result = $db->query('SELECT video FROM videos WHERE `name` = "'.$video.'"')) !== false) {
        echo '<div content="Content-Type: video/mp4">
                  <video width="700" height="450" controls="controls" poster="image" preload="metadata">
                  <source src="data:video/mp4;base64,'.base64_encode($result->fetch(PDO::FETCH_COLUMN)).'"/>;
              </video>
          </div>';
    } else {
        // actions to take if it fails
    }
}

Upvotes: 1

intgr
intgr

Reputation: 20466

You shouldn'd be fetching the whole video into memory from the database at once, because it will stay in memory the whole time while the client is playing/downloading it. With more concurrent users, every PHP backend will keep its own copy of the video in memory. This will make your server run out of memory pretty quickly.

You could use substr() in the SQL query to fetch it in chunks, but that would put more load on the database. Better to split it in chunks before inserting it to the database.

Or, you know, just serve it directly from the file system with Apache (or any web server), like $deity intended.

Upvotes: 1

Paulo Freitas
Paulo Freitas

Reputation: 13649

In case you really want to store videos as databases blobs (I don't recommend so), this should work:

<?php
mysql_connect('localhost', 'root', '46') or die('Could not connect to MySQL server');
mysql_select_db('test') or die('Could not select database');

if (($result = mysql_query('SELECT video FROM test2')) !== false) {
    header('Content-Type: video/mp4'); 
    print mysql_result($result, 0);
} else {
    // actions to take if it fails
}

Even better you might want to use PDO to query the database:

<?php
$db = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', '46');

if (($result = $db->query('SELECT video FROM test2')) !== false) {
    header('Content-Type: video/mp4');
    print $result->fetch(PDO::FETCH_COLUMN);
} else {
    // actions to take if it fails
}

Upvotes: 0

Related Questions