Store code in mysql?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
sierra467
Forum Newbie
Posts: 5
Joined: Tue Aug 07, 2007 10:58 am

Store code in mysql?

Post by sierra467 »

I am wondering if it is possible to store php and HTML in a mysql database to be used in a PHP file.

What I am trying to do is to store "<a $attrib_list>\n\tTextLink\n</a>\n" in a mysql field named "syntax" database.

Then in a php file,
1) Create and populate the variable $attrib_list
2) Create a loop to pull out the syntax field data as specified above and assign it to $syntax below the variable $attribute_list
3) Use print statement to out the variable $syntax which I was hoping would be populated with the variables contents of $attrib_list.

So lets say this:

Code: Select all

$attrib_list = ' href="../index.html" title="index"';
...
while ($row = mysql_fetch_array($result)){
    $syntax = $row['syntax'];
}
...
print "<html>\n";
print "<head><title></title></head>\n";
print "<body>\n";
print $syntax;
print "</body>\n</html>\n";
I want the source code of the page in the browser to look like this:

Code: Select all

<html>
<head><title></title></head>
<body>
<a  href="../index.html" title="index">
    TextLink
</a>
</body>
</html>
Is it possible?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Store code in mysql?

Post by John Cartwright »

I would advise you to never store php code in the database.. that is just scary! However, yes it is possible by passing the string of php code through eval().
sierra467
Forum Newbie
Posts: 5
Joined: Tue Aug 07, 2007 10:58 am

Re: Store code in mysql?

Post by sierra467 »

ok, thanks for the warning. I will try to dream up another way to do it. Thanks for the eval() heads up.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Store code in mysql?

Post by Kieran Huggins »

sounds like a job for "view helpers" - little functions that build HTML in your template:

Code: Select all

function link_to($text,$url){
  return('<a href="'.$url.'">'.$text.'</a>');
}
You could get fancy by accepting an array of attributes as well, but I'll stop here.
Post Reply