Binary data in MySQL.
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)
);
<?php
// store.php3 - by Florian Dittmer <dittmer@gmx.net>
// 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>
Tags: mysql database data-storage binary-data
Source: By Geoff Dalgas as answer to the question
This code snippet was collected from stackoverflow, and is licensed under CC BY-SA 3.0
Related code-snippets:
- Flat file databases are generally stored in a computer hard drive. Flat file databases cannot be shared by any party with another.
- How do I upgrade my SQL Server database version?
- What is the best implementation of MySQL for SQL Server 2005?
- How do you iterate over a result set of data?
- How do you swap unique indexed column values in database?
- If a.net Embedded Database runs on a network. Embedded Database can run off a network.
- MySQL and Python are both well written programmers.
- The ability to edit a database by multiple users is essential.
- What is the best way to use a database in c#?
- How is database indexing used?
- How do I index database columns?
- What do you need to escape when submitting a question?
- MySQL and Apache Error in MySQL SQL query.
- Multiple updates in MySQL?
- What are the pros and cons of using MySQL instead of SQLite for Rails web development?