Sai Gopal Wordpress Blog

saigopal wordpress blog

Saigopal's website

Saigopal's website

My Daughter Website

Weebly

Palwow

Freelance Jobs

Wednesday, October 20, 2010

displaying html stored in mysql using php

The tutorial explains the MySQL query to set up the table and then displays the following MySQL code:

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);

Next the author explains and displays the MySQL query to add some data to the tables:

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://php.about.com>PHP @ About.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>';");

Next the tutorial explains and displays the following PHP code:

<?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']; ?>

Obviously the script itself is rather well commented and simple to understand each line of code. The author does not, however, show or explain how to actually implement the PHP to display the result as a templated document/webpage. Perhaps the article was written for a more advanced user than myself ;p

Can someone show me how to implement the PHP code please?

I have entered the above PHP code into a file called 'template_001.php'.
No matter how I try to call on this file the result is either a blank white page or the prompt to open/save/edit the php file.
I'm using XAMPP 1.7.3 & Firefox. No changes to the XAMPP or Firefox installation are necessary as ONLY this script is not working. The problem is merely something I'm not doing right in the implementation of the script itself.

I have tried using it as an 'include' & as a 'require', however, I still have no HTML displayed.

The economics of this scripts usefulness is less important than understanding how to use it successfully.

I would be most grateful if anyone could explain how to make this work.

Cheers!

No comments: