Binary Data in MySQL

Just how do I store binary data in MySQL?

0
2019-05-07 00:21:28
Source Share
Answers: 3

The solution by phpguy is proper yet I assume there is a great deal of complication in the added information there.

The standard solution remains in a BLOB information type/ feature domain name. BALL is brief for Binary Large Object which column information type specifies for taking care of binary data.

See the relevant manual page for MySQL.

0
2019-05-08 19:51:38
Source

For a table similar to this :

CREATE TABLE binary_data (
    id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    description CHAR(50),
    bin_data LONGBLOB,
    filename CHAR(50),
    filesize CHAR(50),
    filetype CHAR(50)
);

Here is a PHP instance :

<?php
    // store.php3 - by Florian Dittmer <[email protected]>
    // Example php script to demonstrate the storing of binary files into
    // an sql database. More information can be found at http://www.phpbuilder.com/
?>

<html>
    <head><title>Store binary data into SQL Database</title></head>

    <body>
        <?php
            // Code that will be executed if the form has been submitted:

            if ($submit) {
                // Connect to the database (you may have to adjust
                // the hostname, username or password).

                mysql_connect("localhost", "root", "password");
                mysql_select_db("binary_data");

                $data = mysql_real_escape_string(fread(fopen($form_data, "r"), filesize($form_data)));

                $result = mysql_query("INSERT INTO binary_data (description, bin_data, filename, filesize, filetype) ".
                                    "VALUES ('$form_description', '$data', '$form_data_name', '$form_data_size', '$form_data_type')");

                $id= mysql_insert_id();
                print "<p>This file has the following Database ID: <b>$id</b>";

                mysql_close();
            } else {

                // else show the form to submit new data:
        ?>
        <form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">
            File Description:<br>
            <input type="text" name="form_description"  size="40">
            <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
            <br>File to upload/store in database:<br>
            <input type="file" name="form_data"  size="40">
            <p><input type="submit" name="submit" value="submit">
        </form>

        <?php
            }
        ?>
    </body>
</html>
0
2019-05-08 19:50:11
Source

While you have not claimed what you're saving, and also you might have a wonderful factor for doing so, usually the solution is 'as a filesystem reference' and also the real information gets on the filesystem someplace.

http://www.onlamp.com/pub/a/onlamp/2002/07/11/MySQLtips.html

0
2019-05-07 16:46:39
Source