<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>0t@k-B@L! &#187; mysql</title>
	<atom:link href="http://www.otakbali.com/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.otakbali.com</link>
	<description>think about web programming</description>
	<lastBuildDate>Mon, 02 Aug 2010 03:56:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Display Images from mySQL Binary Data</title>
		<link>http://www.otakbali.com/display-images-from-mysql-binary-data.html</link>
		<comments>http://www.otakbali.com/display-images-from-mysql-binary-data.html#comments</comments>
		<pubDate>Tue, 10 Nov 2009 15:11:14 +0000</pubDate>
		<dc:creator>andi</dc:creator>
				<category><![CDATA[Tips n Trik]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[trick]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://otakbali.com/?p=63</guid>
		<description><![CDATA[This tutorial will show you how to display your images (binary data) which has been saved in your mySQL database. It&#8217;s very very simple.. so.. simple&#8230;. (read also: Storing Images/Binary Files to mySQL in PHP) First of all we must connect to our database PHP Code: &#60;?php $username = ""; $password = ""; $host = [...]]]></description>
			<content:encoded><![CDATA[<div id="post_message_41500">This tutorial will show you how to display your images (binary data) which has been saved in your mySQL database.</div>
<div>It&#8217;s very very  simple.. so.. simple&#8230;. (read also: <a href="http://otakbali.com/storing-imagesbinary-files-to-mysql-in-php/"><strong>Storing Images/Binary Files to mySQL in PHP</strong></a>)</p>
<p>First of all we must connect to our database</p></div>
<div id="post_message_41500">
<div style="margin: 5px 20px 20px;">
<div class="smallfont" style="margin-bottom: 2px;">PHP Code:</div>
<div class="alt2" style="border: 1px inset; margin: 0px; padding: 6px; overflow: auto; width: 500px; height: 114px; text-align: left;" dir="ltr"><code><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php<br />
$username </span><span style="color: #007700;">= </span><span style="color: #dd0000;">""</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$password </span><span style="color: #007700;">= </span><span style="color: #dd0000;">""</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$host </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"localhost"</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$database </span><span style="color: #007700;">= </span><span style="color: #dd0000;">""</span><span style="color: #007700;">;</span></p>
<p></span></code></div>
</div>
<p><strong><span id="more-63"></span>$username = &#8220;&#8221;;</strong> &#8211; It&#8217;s your database&#8217;s username<strong><br />
$password = &#8220;&#8221;;</strong> &#8211; It&#8217;s your database&#8217;s password<strong><br />
$host = &#8220;localhost&#8221;;</strong> &#8211; It&#8217;s your database&#8217;s host, usually it&#8217;s localhost but it can be something else also<strong><br />
$database = &#8220;&#8221;;</strong> &#8211; It&#8217;s your database</p>
<p>Now let&#8217;s connect to the database</p>
<div style="margin: 5px 20px 20px;">
<div class="smallfont" style="margin-bottom: 2px;">PHP Code:</div>
<div class="alt2" style="border: 1px inset; margin: 0px; padding: 6px; overflow: auto; width: 500px; height: 66px; text-align: left;" dir="ltr"><code><span style="color: #000000;"><span style="color: #0000bb;"> </span><span style="color: #007700;">@</span><span style="color: #0000bb;">mysql_connect</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$host</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$username</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$password</span><span style="color: #007700;">) or die(</span><span style="color: #dd0000;">"Can not connect to database: "</span><span style="color: #007700;">.</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());<br />
@</span><span style="color: #0000bb;">mysql_select_db</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$database</span><span style="color: #007700;">) or die(</span><span style="color: #dd0000;">"Can not select the database: "</span><span style="color: #007700;">.</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">()); </span></p>
<p></span><span style="color: #0000bb;"> </span></code></div>
</div>
<p><strong>@mysql_connect($host, $username, $password) or die(&#8220;Can not connect to database:&#8221;.mysql_error());</strong> &#8211; This connects to your database<br />
<strong><br />
@mysql_select_db($database) or die(&#8220;Can not select the database: &#8220;.mysql_error());</strong> &#8211; This will select your database</p>
<p>Now let&#8217;s get our id</p>
<div style="margin: 5px 20px 20px;">
<div class="smallfont" style="margin-bottom: 2px;">PHP Code:</div>
<div class="alt2" style="border: 1px inset; margin: 0px; padding: 6px; overflow: auto; width: 500px; height: 34px; text-align: left;" dir="ltr"><code><span style="color: #000000;"><span style="color: #0000bb;">$id </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$_GET</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'id'</span><span style="color: #007700;">]; </span><span style="color: #0000bb;"> </span></span></code></div>
</div>
<p>It will get a id from an URL. <a href="http://blabla.com" target="_blank">blabla.com</a></p>
<p>So.. your id will be 3 and it will show an image which id is 3</p>
<div style="margin: 5px 20px 20px;">
<div class="smallfont" style="margin-bottom: 2px;">PHP Code:</div>
<div class="alt2" style="border: 1px inset; margin: 0px; padding: 6px; overflow: auto; width: 500px; height: 66px; text-align: left;" dir="ltr"><code><span style="color: #000000;"><span style="color: #0000bb;"> </span><span style="color: #007700;">if(!isset(</span><span style="color: #0000bb;">$id</span><span style="color: #007700;">) || empty(</span><span style="color: #0000bb;">$id</span><span style="color: #007700;">)){<br />
die(</span><span style="color: #dd0000;">"Please select your image!"</span><span style="color: #007700;">);<br />
}else{</span></span></code></div>
</div>
<p><strong>if(!isset($id) || empty($id)){</strong> &#8211; If this ID in your url is empty like <em>?id=</em> of if it&#8217;s not even set<strong><br />
die(&#8220;Please select your image!&#8221;);</strong> &#8211; lets display an error<strong><br />
}else{</strong> &#8211; But if it is set let&#8217;s continue with showing our image</p>
<p><img class="tcattdimgresizer" src="http://forum.codecall.net/images/Tutorials/jaan/5x0vq0.jpg" border="0" alt="" /></p>
<div style="margin: 5px 20px 20px;">
<div class="smallfont" style="margin-bottom: 2px;">PHP Code:</div>
<div class="alt2" style="border: 1px inset; margin: 0px; padding: 6px; overflow: auto; width: 500px; height: 66px; text-align: left;" dir="ltr"><code style="white-space: nowrap;"><br />
<!-- php buffer start --></code><code><span style="color: #000000;"><span style="color: #0000bb;">$query </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"SELECT * FROM tbl_images WHERE id='"</span><span style="color: #007700;">.</span><span style="color: #0000bb;">$id</span><span style="color: #007700;">.</span><span style="color: #dd0000;">"'"</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$row </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_fetch_array</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$query</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$content </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'image'</span><span style="color: #007700;">]; </span></p>
<p></span></code></div>
</div>
<p><strong>$query = mysql_query(&#8220;SELECT * FROM tbl_images WHERE id=&#8217;&#8221;.$id.&#8221;&#8216;&#8221;);</strong> &#8211; Now let&#8217;s select the blob from the table where id is $id (for example: 3)</p>
<p><strong>$row = mysql_fetch_array($query);</strong> &#8211; Let&#8217;s gather our info about image which id is $id into one variable</p>
<p><strong>$content = $row['image'];</strong> &#8211; Get&#8217;s the blob from our table<br />
Now let&#8217;s display our image</p>
<div style="margin: 5px 20px 20px;">
<div class="smallfont" style="margin-bottom: 2px;">PHP Code:</div>
<div class="alt2" style="border: 1px inset; margin: 0px; padding: 6px; overflow: auto; width: 500px; height: 82px; text-align: left;" dir="ltr"><code><span style="color: #000000;"><span style="color: #0000bb;">header</span><span style="color: #007700;">(</span><span style="color: #dd0000;">'Content-type: image/jpg'</span><span style="color: #007700;">);<br />
echo </span><span style="color: #0000bb;">$content</span><span style="color: #007700;">;<br />
} </span></span></code></div>
</div>
<p><strong>header(&#8216;Content-type: image/jpg&#8217;);</strong> &#8211; This tells to the browser and to the server that this file will be a jpg file</p>
<p><strong>echo $content;</strong> &#8211; This will display our blob..</p>
<p><strong>}</strong> &#8211; Ends else<br />
Okay.. now here&#8217;s our full script.</p>
<div style="margin: 5px 20px 20px;">
<div class="smallfont" style="margin-bottom: 2px;">PHP Code:</div>
<div class="alt2" style="border: 1px inset; margin: 0px; padding: 6px; overflow: auto; width: 500px; height: 450px; text-align: left;" dir="ltr"><code><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php</p>
<p>$username</p>
<p></span><span style="color: #007700;">= </span><span style="color: #dd0000;">""</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$password </span><span style="color: #007700;">= </span><span style="color: #dd0000;">""</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$host </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"localhost"</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$database </span><span style="color: #007700;">= </span><span style="color: #dd0000;">""</span><span style="color: #007700;">;</p>
<p>@</p>
<p></span><span style="color: #0000bb;">mysql_connect</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$host</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$username</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$password</span><span style="color: #007700;">) or die(</span><span style="color: #dd0000;">"Can not connect to database: "</span><span style="color: #007700;">.</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());</p>
<p>@</p>
<p></span><span style="color: #0000bb;">mysql_select_db</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$database</span><span style="color: #007700;">) or die(</span><span style="color: #dd0000;">"Can not select the database: "</span><span style="color: #007700;">.</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());</p>
<p></span><span style="color: #0000bb;">$id </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$_GET</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'id'</span><span style="color: #007700;">];</p>
<p>if(!isset(</p>
<p></span><span style="color: #0000bb;">$id</span><span style="color: #007700;">) || empty(</span><span style="color: #0000bb;">$id</span><span style="color: #007700;">)){<br />
die(</span><span style="color: #dd0000;">"Please select your image!"</span><span style="color: #007700;">);<br />
}else{</p>
<p></span><span style="color: #0000bb;">$query </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"SELECT * FROM tbl_images WHERE id='"</span><span style="color: #007700;">.</span><span style="color: #0000bb;">$id</span><span style="color: #007700;">.</span><span style="color: #dd0000;">"'"</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$row </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_fetch_array</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$query</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$content </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'image'</span><span style="color: #007700;">];</p>
<p></span><span style="color: #0000bb;">header</span><span style="color: #007700;">(</span><span style="color: #dd0000;">'Content-type: image/jpg'</span><span style="color: #007700;">);<br />
echo </span><span style="color: #0000bb;">$content</span><span style="color: #007700;">;</p>
<p>}</p>
<p></span><span style="color: #0000bb;">?&gt;</span></span> </code><!-- php buffer end --></div>
</div>
<p>It works perfectly</p>
<p><img class="tcattdimgresizer" src="http://forum.codecall.net/images/Tutorials/jaan/ingvvq.jpg" border="0" alt="" /></p>
<p>Enjoy&#8230; <img src='http://www.otakbali.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />      If you have questions then please feel free to ask</div>
]]></content:encoded>
			<wfw:commentRss>http://www.otakbali.com/display-images-from-mysql-binary-data.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Storing Images/Binary Files to mySQL in PHP</title>
		<link>http://www.otakbali.com/storing-imagesbinary-files-to-mysql-in-php.html</link>
		<comments>http://www.otakbali.com/storing-imagesbinary-files-to-mysql-in-php.html#comments</comments>
		<pubDate>Tue, 10 Nov 2009 13:58:06 +0000</pubDate>
		<dc:creator>andi</dc:creator>
				<category><![CDATA[Tips n Trik]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[trick]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://otakbali.com/?p=59</guid>
		<description><![CDATA[how to store images, audio, or another binary files into mySQL database with PHP script. cara menyimpan gambar ke mysql dengan PHP.]]></description>
			<content:encoded><![CDATA[<p>It is possible for us to store/save our images (binary files) into a mySQL database in PHP. In this article, we will discuss about &#8220;how to store images, audio, or another binary files into mySQL database with PHP script&#8221;. But, before we continue to the further step, you might want to consider some reason of why we should and why we shouldn&#8217;t store binary files in a database:</p>
<p>Reasons to store your images (binary files) in a database:</p>
<ol style="list-style-type: decimal;">
<li>Storing in a database allows better security for your files and images.</li>
<li>Eliminates messy directory and file structures (this is especially true when users are allow to contribute files).</li>
<li>Files/Images can be stored directly linking to user or advertiser information.<span id="more-59"></span></li>
</ol>
<p>You may want to reconsider storing your binary files in the database.  Here are some reason you may not want to:</p>
<ol style="list-style-type: decimal;">
<li>You can&#8217;t directly access your files using standard applications such as FTP.</li>
<li>If you database becomes corrupt, so do your files.</li>
<li>Migrating to a new database is made more difficult</li>
<li>Pulling images from a database and displaying them is slower than using file access.</li>
</ol>
<p>After considering above reasons, you may continue to the first step of this tutorial.</p>
<p><span style="font-size: medium;">Database Type and Connection</span></p>
<p>I assume that you already create a mySQL database named &#8220;binary&#8221;. We created two tables, one the primary ID (of the row/entry) and the binary BLOB. A BLOB is a binary large object that can hold a variable amount of data. The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. These differ only in the maximum length of the values they can hold. The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. These correspond to the four BLOB types and have the same maximum lengths and storage requirements.</p>
<p>You&#8217;ve now created your database and are ready to upload binary files to it. In this case, images. Next we will create a simple upload script.</p>
<p><span style="font-size: medium;">Creating the HTML</span></p>
<p>Creating the user interface where you or your visitors will upload files is very simple.  Using basic HTML and relying on the browser to do most of the heavy lifting you can easily create an upload form.  Here is the one we will use:</p>
<div style="margin: 5px 20px 20px;">
<div class="smallfont" style="margin-bottom: 2px;">HTML Code:</div>
<pre class="alt2" style="border: 1px inset; margin: 0px; padding: 6px; overflow: auto; width: 500px; height: 82px; text-align: left;" dir="ltr"><span style="color: #ff8000;">&lt;form enctype=<span style="color: #0000ff;">"multipart/form-data"</span> action=<span style="color: #0000ff;">"insert.php"</span> method=<span style="color: #0000ff;">"post"</span> name=<span style="color: #0000ff;">"changer"</span>&gt;</span>

<span style="color: #ff8000;">&lt;input name=<span style="color: #0000ff;">"MAX_FILE_SIZE"</span> value=<span style="color: #0000ff;">"102400"</span> type=<span style="color: #0000ff;">"hidden"</span>&gt;</span>
<span style="color: #ff8000;">&lt;input name=<span style="color: #0000ff;">"image"</span> accept=<span style="color: #0000ff;">"image/jpeg"</span> type=<span style="color: #0000ff;">"file"</span>&gt;</span>

<span style="color: #ff8000;">&lt;input value=<span style="color: #0000ff;">"Submit"</span> type=<span style="color: #0000ff;">"submit"</span>&gt;</span></pre>
</div>
<p>Copy this code into a file and save it as <strong>add.html</strong></p>
<p>The code above allows the uses to browse for a file and select any image/jpeg file type.   Once selected and the submit button is pressed the image contents are forwarded to PHP script, <strong>insert.php</strong></p>
<p><span style="font-size: medium;"><strong>Inserting the Image into MySQL</strong></span></p>
<p>Create a new file named insert.php.  If you change the name of this file you will also need to change the action=&#8221;" part in your HTML file above.  Open the file in your favorite text editor or PHP IDE.</p>
<p>Write or copy/paste this code into your file:</p>
<div style="margin: 5px 20px 20px;">
<div class="smallfont" style="margin-bottom: 2px;">PHP Code:</div>
<div class="alt2" style="border: 1px inset; margin: 0px; padding: 6px; overflow: auto; width: 500px; height: 274px; text-align: left;" dir="ltr"><code style="white-space: nowrap;"><br />
<!-- php buffer start --></code><code><span style="color: #000000;"><br />
<span style="color: #0000bb;"> </span><span style="color: #ff8000;">// Create MySQL login values and<br />
// set them to your login information.<br />
</span><span style="color: #0000bb;">$username </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"YourUserName"</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$password </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"YourPassword"</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$host </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"localhost"</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$database </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"binary"</span><span style="color: #007700;">;</p>
<p></span><span style="color: #ff8000;">// Make the connect to MySQL or die<br />
// and display an error.<br />
</span><span style="color: #0000bb;">$link </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_connect</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$host</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$username</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$password</span><span style="color: #007700;">);<br />
if (!</span><span style="color: #0000bb;">$link</span><span style="color: #007700;">) {<br />
die(</span><span style="color: #dd0000;">'Could not connect: ' </span><span style="color: #007700;">. </span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());<br />
}</p>
<p></span><span style="color: #ff8000;">// Select your database<br />
</span><span style="color: #0000bb;">mysql_select_db </span><span style="color: #007700;">(</span><span style="color: #0000bb;">$database</span><span style="color: #007700;">);</p>
<p></span><span style="color: #0000bb;"> </span><br />
</span><br />
</code><!-- php buffer end --></div>
</div>
<p>The above code sets your connection variables, creates a connection to MySQL and selects your database (binary if you followed my instructions above).  Next we need to read the form data and insert it into the database.</p>
<div style="margin: 5px 20px 20px;">
<div class="smallfont" style="margin-bottom: 2px;">PHP Code:</div>
<div class="alt2" style="border: 1px inset; margin: 0px; padding: 6px; overflow: auto; width: 500px; height: 498px; text-align: left;" dir="ltr">
<p><code style="white-space: nowrap;"><br />
<!-- php buffer start --></code><code><span style="color: #000000;"><br />
<span style="color: #0000bb;"> </span><span style="color: #ff8000;">// Make sure the user actually<br />
// selected and uploaded a file<br />
</span><span style="color: #007700;">if (isset(</span><span style="color: #0000bb;">$_FILES</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'image'</span><span style="color: #007700;">]) &amp;&amp; </span><span style="color: #0000bb;">$_FILES</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'image'</span><span style="color: #007700;">][</span><span style="color: #dd0000;">'size'</span><span style="color: #007700;">] &gt; </span><span style="color: #0000bb;">0</span><span style="color: #007700;">) {</span></span></code></p>
<p><span style="color: #ff8000;">// Temporary file name stored on the server<br />
</span><span style="color: #0000bb;">$tmpName </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$_FILES</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'image'</span><span style="color: #007700;">][</span><span style="color: #dd0000;">'tmp_name'</span><span style="color: #007700;">];</span></p>
<p><span style="color: #ff8000;">// Read the file<br />
</span><span style="color: #0000bb;">$fp </span><span style="color: #007700;">= </span><span style="color: #0000bb;">fopen</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$tmpName</span><span style="color: #007700;">, </span><span style="color: #dd0000;">&#8216;r&#8217;</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">$data </span><span style="color: #007700;">= </span><span style="color: #0000bb;">fread</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$fp</span><span style="color: #007700;">, </span><span style="color: #0000bb;">filesize</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$tmpName</span><span style="color: #007700;">));<br />
</span><span style="color: #0000bb;">$data </span><span style="color: #007700;">= </span><span style="color: #0000bb;">addslashes</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$data</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">fclose</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$fp</span><span style="color: #007700;">);</span></p>
<p><span style="color: #ff8000;">// Create the query and insert<br />
// into our database.<br />
</span><span style="color: #0000bb;">$query </span><span style="color: #007700;">= </span><span style="color: #dd0000;">&#8220;INSERT INTO tbl_images &#8221;</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$query </span><span style="color: #007700;">.= </span><span style="color: #dd0000;">&#8220;(image) VALUES (&#8216;$data&#8217;)&#8221;</span><span style="color: #007700;">;<br />
</span><span style="color: #0000bb;">$results </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$query</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$link</span><span style="color: #007700;">);</span></p>
<p><span style="color: #ff8000;">// Print results<br />
</span><span style="color: #007700;">print </span><span style="color: #dd0000;">&#8220;Thank you, your file has been uploaded.&#8221;</span><span style="color: #007700;">;</span></p>
<p>}<br />
else {<br />
print <span style="color: #dd0000;">&#8220;No image selected/uploaded&#8221;</span><span style="color: #007700;">;<br />
}</span></p>
<p><span style="color: #ff8000;">// Close our MySQL Link<br />
</span><span style="color: #0000bb;">mysql_close</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$link</span><span style="color: #007700;">);<br />
</span><span style="color: #0000bb;">?&gt;</span></p>
<p><!-- php buffer end --></div>
</div>
<p>The PHP code above takes the user selected image, reads it with fopen and stores the data in a variable named $data.  The binary data is then inserted into our database for retrieval at a later time.</p>
<p>Save the file above and access add.html via your browser.  Select an image file and upload it.</p>
<p>I will post the next stage of this tutorial: &#8220;<a href="http://otakbali.com/display Images-from-mySQL-binary-data"><strong>How to read binary data from database</strong></a>&#8220;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.otakbali.com/storing-imagesbinary-files-to-mysql-in-php.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
