<?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>Reflection Media &#187; mysql</title>
	<atom:link href="http://www.reflectionmedia.ro/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.reflectionmedia.ro</link>
	<description>Custom WordPress Solutions</description>
	<lastBuildDate>Tue, 24 Jan 2012 10:34:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Fun with PHP</title>
		<link>http://www.reflectionmedia.ro/2009/02/16/fun-with-php/</link>
		<comments>http://www.reflectionmedia.ro/2009/02/16/fun-with-php/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 10:09:51 +0000</pubDate>
		<dc:creator>Cristian</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.reflectionmedia.ro/?p=199</guid>
		<description><![CDATA[OK. So we know that instead of using the database, regular files can be used. It&#8217;s quite ineffective, but it&#8217;s possible. This means that the database structure and the regular file structure are somehow interchangeable. So&#8230; this would mean that the database can be used instead of regular files. Now, this is the obvious use [...]]]></description>
			<content:encoded><![CDATA[<p>OK. So we know that instead of using the database, regular files can be used. It&#8217;s quite ineffective, but it&#8217;s possible. This means that the database structure and the regular file structure are somehow interchangeable.</p>
<p>So&#8230; this would mean that the database can be used instead of regular files.</p>
<p>Now, this is the obvious use of the BLOB object, but it&#8217;s mainly used for media files. What I had in mind are actual web-pages, including PHP code, stored inside the database.<br />
<span id="more-199"></span><br />
The first obstacle I bumped into was that you can&#8217;t echo PHP code. I mean, you can do it, but it&#8217;s useless. The web server won&#8217;t parse your PHP twice, so it just sends PHP code to your browser, which is dumb.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt; ?php echo 'hello world'; ?&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// does not work</span></pre></td></tr></table></div>

<p>The first solution that came to mind was writing the code in a temporary file and right after that include it, <em>in the very same script</em>. I&#8217;d love to hear other solutions to this problem.</p>
<p>So, once I got this figured out, the rest was more or less a piece of cake.</p>
<p>All I had to do was:
<ul>
<li>Connect to the MySQL server, select the database and fetch the results;</li>
<li>Create a temporary file, write the database results there and then close it;</li>
<li>Include the temporary file right there, in the same script;</li>
<li>Delete the temporary file and close the MySQL connection.</li>
</ul>
<p>The table I used is a very simple one:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">CREATE TABLE <span style="color: #b1b100;">IF</span> NOT EXISTS `webpages` <span style="color: #009900;">&#40;</span>
  `name` varchar<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">default</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span>
  `content` varchar<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">default</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And here is the little script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// a function I made for easily linking to other database web pages:</span>
<span style="color: #000000; font-weight: bold;">function</span> dblink<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'http://'</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SERVER_NAME'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'SCRIPT_NAME'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'?name='</span><span style="color: #339933;">.</span><span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// function for including the text of a database web page:</span>
<span style="color: #000000; font-weight: bold;">function</span> dbinclude<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// getting the content of the web page from the database:</span>
	<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM webpages WHERE name='<span style="color: #006699; font-weight: bold;">$name</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// creating a tempoeary file and writing the web page content to it:</span>
	<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;temp.php&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// include the text of the temporary file:</span>
	<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'temp.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// get the name of the database web page I want to include:</span>
<span style="color: #000088;">$name</span><span style="color: #339933;">=</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">==</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$name</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'home'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// connect to the database:</span>
<span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;your_username&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;your_password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;your_database&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// include the content of the database web page:</span>
dbinclude<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// close the MySQL connection and delete the temporary file:</span>
<span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'temp.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>It&#8217;s the only file a site needs. Everything else can be in the database. Of course, I do realize that the database too is made of files.</p>
<p>You can test it <a href="http://www.reflectionmedia.ro/projects/littlescript/">here</a> and you can download the script and sql file from <a href="http://www.reflectionmedia.ro/projects/littlescript/files.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reflectionmedia.ro/2009/02/16/fun-with-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

