+ Reply to Thread
Results 1 to 3 of 3

Thread: Store PHP in MySQL?

  1. #1

    Default Store PHP in MySQL?

    I know that you are able to put html in a database...

    I am trying to put php in my database so that I can pull php scripts from my database. Is it possible to do this?

    Example... when I add <font color="green">This is a test</font> to a table in my database, the website pulls it just fine. However, if I put echo "This is a test"; in my database it will not pull it.

    I need to put php inside my database

  2. #2
    Ruby Star member nobita_deptrai is on a distinguished road
    Join Date
    Jun 2011
    Posts
    458

    Default

    You can refer this exam of me .
    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 demonstrate this 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://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>';");

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

    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.
    BDS Real Estate Company
    Go to my website rao vat ban nha to learn about real estate in Vietnam: Home sales , Apartment for sale, Feng Shui ...

  3. #3
    Sapphire Star member anusha is on a distinguished road
    Join Date
    Sep 2011
    Posts
    56

    Default

    In order to parse stored php code, you would need to run it as a string through the eval() function. This site stores the templates files (including php) within a database.

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts