Function stored in database is not executing.

I have following code for output

$q = mysql_query("SELECT * FROM pages WHERE page_id='$id'");
$r = mysql_fetch_object($q);

    //print the pages content
    echo "<h1>$r->page_title</h2>";
    echo $r->page_content;

page_title & page_content are database column

page_content column have following data

slider(); // it may be text or function OR both text + function

slider function

 function slider(){
     echo   "Hey! You have Slider";// query to display slider images from database



Output Expected:
Hey! You have Slider

Recommended Answers

All 5 Replies

Page_content is a string that is outputted to the browser. It will not interpret any code. If you really want that to happen, you can use eval(), but be very careful with it.

output of eval

echo eval(slider());

$slider = eval(slider());

this appears at top of the page

Full Code

 <div id="content">
    //if no page clicked on load home page default to it of 1
        $q = mysql_query("SELECT * FROM pages WHERE page_id='1'");
    } else { //load requested page based on the id
        $id = $_GET['p']; //get the requested id
        $id = mysql_real_escape_string($id); //make it safe for database use
        $q = mysql_query("SELECT * FROM pages WHERE page_id='$id'");

    //get page data from database and create an object
    $r = mysql_fetch_object($q);

    //print the pages content
    echo "<h1>$r->page_title</h2>";
    echo $r->page_content;

Member Avatar

1) Any reason why you're using deprecated mysql functions instead of PDO/mysqli?
2) Why store code (object) in DB instead of an include file?

As per My clients requirements I have created database pages. So all pages are created dynamically.
Client don't want any CMS.

DB is like following

TABLE pages

page_id          int
page_title       varchar
pageCont         text
parent           int
sort_order       int
status           enum('A','I')  //active or inactive

This creates pages required for building website. pageCont field have content which will appear on respective pages.
Client wants to add pages as per his requirements own his own.

So, there is another table created to display gallery images, gallery & another one is slider
Whenever client create pages he will just call function gallery() OR slider() on respective pages.

e.g. table pages have record,

    page_title   =>  Home Page
    pageCont     => slider()

This is expected to show slider on home page

    page_title   =>  Gallery
    pageCont     => gallery()

This is expected to show gallery on gallery page

While I was working I found it is simillar to wp short codes
I used eval() but, if there is no funtion & have text content then it is not working well.

How to create & manage .php pages like gallery.php with adding field page_alias in table pages?


page_title       =>Get In Touch 
pageCont         =>Contat Info + Custfunction()
page_alias       =>*contact*

page_alias will generate contact.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.