{"id":676,"date":"2010-02-09T20:22:23","date_gmt":"2010-02-09T20:22:23","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=676"},"modified":"2010-02-09T20:22:23","modified_gmt":"2010-02-09T20:22:23","slug":"blob-to-store-data-in-mysql-database","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/02\/09\/blob-to-store-data-in-mysql-database\/","title":{"rendered":"Blob to store data in mysql database"},"content":{"rendered":"<p>To store data within a blob in a database can be a good thing at times because then you can just copy the database from one place to another and use the access rights on the database to restrict access to the &#8220;files&#8221; within the database.<\/p>\n<p>There could be a few reasons why you want to store the data within a blob in the database, but here how the basics would work.<\/p>\n<p>To start with you have to create a database and a table to store the data\/file within the blob, of course if you have already created the database and\/or the tables then alter as you think, but here is the basics.<\/p>\n<pre lang=\"sql\">\r\n  create database phptestplace;\r\n  create table storingData (id int not null auto_incremenet, lblob blob, primary key (id));\r\n<\/pre>\n<p>And then within a php file you can access the database and a file.<\/p>\n<pre lang=\"php\">\r\n  $link = mysql_connect('localhost', 'username', 'userpassword');\r\n  if (!$link) {\r\n      die('Could not connect: ' . mysql_error());\r\n  }\r\n\/\/ alter to your database name\r\n  mysql_select_db(\"phptestplace\", $link);\r\n<\/pre>\n<p>and now access the file and read in file<\/p>\n<pre lang=\"php\">\r\n  $filename = \"filetoload.txt\";\r\n  $handle = fopen($filename, \"r\");\r\n  $contents = fread($handle, filesize($filename));\r\n  fclose($handle);\r\n\r\n\/\/ to insert into the database you need to add in the slashes for characters like \/ \\ etc.\r\n  $contents = addslashes($contents);\r\n<\/pre>\n<p>to insert into the blob you just, change the table and table name to what may have called it.<\/p>\n<pre lang=\"php\">\r\n  $sqlquery = \"insert into storingData(largeblob) values ('$contents')\";\r\n  mysql_query($sqlquery) or die(\"ERROR\");*\/\r\n<\/pre>\n<p>to get the data back  (I am calling back the last inserted value into the table)<\/p>\n<pre lang=\"php\">\r\n\/\/ get the data into a result variable\r\n  $return = mysql_query (\"select lblob from storingData where id = (select max(id) from storingData)\") or die(\"LLL\");\r\n\/\/ get the contents of the blob from the return variable (it returns a array of data) and the list takes out the data from a array each part at time.\r\n  list($newcontents) = mysql_fetch_array($return);\r\n<\/pre>\n<p>and then store the data from the database pull into a file, I have called it newfile.txt, but it is up to you.<\/p>\n<pre lang=\"php\">\r\n  $fp = fopen('NEWFILE.txt', 'w');\r\n  fwrite($fp, $newcontents);\r\n  fclose($fp);\r\n<\/pre>\n<p>Of course can do it via a web page, using a HTML FORM enctype=&#8221;multipart\/form-data&#8221; within the form tag otherwise it may not work.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>To store data within a blob in a database can be a good thing at times because then you can just copy the database from one place to another and use the access rights on the database to restrict access to the &#8220;files&#8221; within the database. There could be a few reasons why you want &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/02\/09\/blob-to-store-data-in-mysql-database\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Blob to store data in mysql database<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,21],"tags":[94,22],"class_list":["post-676","post","type-post","status-publish","format-standard","hentry","category-php","category-sql","tag-blob","tag-mysql"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=676"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/676\/revisions"}],"predecessor-version":[{"id":677,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/676\/revisions\/677"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}