{"id":23,"date":"2009-01-30T07:44:22","date_gmt":"2009-01-30T07:44:22","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=23"},"modified":"2024-02-06T15:15:57","modified_gmt":"2024-02-06T15:15:57","slug":"upload-image-to-mysql-using-php","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/upload-image-to-mysql-using-php","title":{"rendered":"Upload Image to MySQL using PHP"},"content":{"rendered":"\n<p>Upload Image to MySQL using PHP<\/p>\n\n\n\n<p>As a new-bie to php\/mysql, I tried different stuffs. So here I&#8217;m with my php code for Image Upload to MySQL. Its a quite simple code with two php files one to display and one to upload.<\/p>\n\n\n\n<p>For Image Upload code, I&#8217;ve added code download link upload-image-mysql-demo.zip at the end of the page.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE `pix` ( \n `pic_id` int(11) NOT NULL auto_increment,\n `pic_name` varchar(100) NOT NULL,\n `pic_data` longblob NOT NULL,\n PRIMARY KEY\u00a0 (`pic_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=latin1;<\/code><\/pre>\n\n\n\n<p>\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Save images to MySQL using PHP<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* Image.php*\/\n&lt;?php\n $con = mysql_connect(\"127.0.0.1:3306\",\"root\",\"\");\n if (!$con)\n {\n die(\"Could not connect: \" . mysql_error());\n }\n $DB = mysql_select_db(\"test\", $con);\n move_uploaded_file($_FILES&#91;\"uploadedfile\"]&#91;\"tmp_name\"],\"latest.img\");\n $instr = fopen(\"latest.img\",\"rb\");\n $image = addslashes(fread(fopen(\"latest.img\",\"r\"),filesize(\"latest.img\")));\n mysql_query (\"insert into pix (pic_name, pic_data) values (\"myImage\", \"'.$image.'\");\");\n?>\n\n&lt;html>\n&lt;form enctype=\"multipart\/form-data\" method=\"POST\">\n&lt;img src=showImage.php?gim=1 width=500 height=150 alt=\"hell\">\n&lt;br>&lt;hr>\n&lt;input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"100000\" \/>\nChoose a file to upload(&lt;100 KB): &lt;input name=\"uploadedfile\" type=\"file\" \/>&lt;br\/>\n&lt;input type=\"submit\" value=\"submit\" name=\"submit\" \/>\n&lt;\/form>\n&lt;html><\/code><\/pre>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Show image in MySQL using PHP<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*showImage.php*\/\n&lt;?php\n $con = mysql_connect(\"127.0.0.1:3306\",\"root\",\"\");\n if (!$con)\n {\n die(\"Could not connect: \" . mysql_error());\n }\n $DB = mysql_select_db(\"test\", $con);\n $res = @mysql_query(\"select * from pix order by pic_id desc limit 1\");\n if ($row = @mysql_fetch_assoc($res)) \n {\n $title = htmlspecialchars($row&#91;pic_name]);\n $bytes = $row&#91;pic_data];\n }\n header(\"Content-type: image\/jpg\");\n print $bytes;\n mysql_close();\n?><\/code><\/pre>\n\n\n\n<p>Click <a href=\"https:\/\/kedar.nitty-witty.com\/blog\/miscpages\/upload-image-mysql-demo.zip\" title=\"\">here <\/a>to download zip file for above demo of Uploading image to mysql using php.<\/p>\n","protected":false},"excerpt":{"rendered":"Upload Image to MySQL using PHP As a new-bie to php\/mysql, I tried different stuffs. So here I&#8217;m with my php code for Image Upload to MySQL. Its a quite&hellip;\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8,378],"tags":[260,910,911,908,909],"class_list":{"0":"post-23","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-mysql","7":"category-mysql-scripts-mysql","8":"tag-image-upload-mysql","9":"tag-mysql-image-upload","10":"tag-php-image-upload","11":"tag-upload-image-to-mysql","12":"tag-upload-image-using-php"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":2,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":3245,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions\/3245"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}