Storing your website inside your database
Have you ever taught its possible to store entire website pattern, even Templating your website was possible. We are here to show you how one could ever save the entire Template or even the complete infrastructure of your processing inside your own MYSQL database.
Mysql getting the power to store so much of values and getting more powerful and big brands like AKA SUN Micro Systems supporting it (Now Part of Oracle Group). Can you ever imagine the power of the small tiny database can store in the values so much there.
So lets start the way we would go about it only from quatre-bornes.com site
HOW YOU CAN SIMPLY STORE YOUR CONTENT INSIDE YOUR DATABASE
You can hold your entire site, both coding and content, in a MySQL Database. Your PHP file then retrieves this information from the database.
To see how really these things which are said in here are possible we are going to build a very simple site with all the information and coding stored in the MySQL database.
Create the Tables
The first thing we need to do is create the database tables to store this information. Since we are making a very simple site we will only make two tables, one for code and one for content. Execute this code to create the database tables:
CREATE TABLE template (template_id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, Head_Open VARCHAR(60), Head_Close VARCHAR(60), Page_End VARCHAR(60), Date VARCHAR(60));
CREATE TABLE content (content_id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(60), body MEDIUMBLOB);
Add Some Data
Now we need to add data to the tables. If you were adding information on a regular basis, it might be easiest to add it from a web browser. In our case we will just add it in manually to demonstrate how to retrieve it from the database and use it. Remember if you are adding it from the web, you need to consider addslashes and magic quotes.
INSERT INTO content (title, body) VALUES ( “MyPage”, “This is my sample page, where I will use PHP and MySQL to template my website. <p> Here is a list of things I like <ul><li>PHP Code</li><li>MySQL</li><li>
<a href=http://www.quatre-bornes.com>PHP at quatre-bornes.com</a></li></ul><p> That is the end of my content.”) ;
INSERT INTO template (Head_Open, Head_Close, Page_End, Date) VALUES ( “<html><head><title>”, “</title><body>”, “</body></html>”, “$b = time (); print date(‘m/d/y’,$b) . ‘<br>’;”);
We can make additional pages simply by adding additional rows to the table called content. Likewise, we could make additional templates by adding a new row to the table called template.
Build the Page
Now all we need to do is call this information from our PHP file. We can do that like this:
<?php
// Connects to your Database
mysql_connect(“your.hostaddress.com”, “username”, “password”) or die(mysql_error());
mysql_select_db(“Database_Name”) or die(mysql_error());
//This retrieves the template and puts into an array. No where clause is used, because we only have one template in our database.
$coding = mysql_query(“SELECT * FROM template”) or die(mysql_error());
$template = mysql_fetch_array( $coding );
//This retrieves the content and puts into an array. Notice we are calling ID 1, this would change if we wanted to call a page stored on a different row
$text = mysql_query(“SELECT * FROM content WHERE content_id =1″) or die(mysql_error());
$content = mysql_fetch_array( $text );
//Actually puts the code and content on the page
Print $template['Head_Open'];
Print $content['title'];
Print $template['Head_Close'];
//When pulling PHP code we need to use EVAL
Eval ($template['Date']);
Print $content['body'];
Print $template['Page_End'];
?>
EVAL : Eval () is used to evaluate the input string as PHP. It is like using the Echo () function in the sense that it outputs everything, except instead of outputting it as text, it outputs it as PHP code to be executed. One use of this is to store code in a database to execute later
This method is a very effective way of creating a template based site, or executing different PHP on the fly simply by calling different rows from MySQL.


