Php In Mysql

loading php in mysql?

Page 1 of 1

8 Replies - 2308 Views - Last Post: 17 December 2004 - 10:24 AM Rate Topic: -----

#1 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Php In Mysql

Posted 14 December 2004 - 03:07 PM

hi all,

is it posible to load php from a mysql database?
if so how?

like inludes
i have tryed it and the data was in the database but would not load it.
many thx
Is This A Good Question/Topic? 0
  • +

Replies To: Php In Mysql

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Php In Mysql

Posted 14 December 2004 - 03:33 PM

Sure is, you'll want to execute your query and get the result in to a variable, then do this <? eval($result); ?>

The eval function will cause whatever is in the $result variable to be executed as PHP, I do not suggest letting users input PHP in to the database, but this is an excelent technique for building templates and storing them in a database.
Was This Post Helpful? 0
  • +
  • -

#3 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Re: Php In Mysql

Posted 14 December 2004 - 03:55 PM

awesome, thx. no not for users.
although i am not shure i completely understand.
i add this to database? or to page i thinking.
<? eval($result); ?>
were to put what?
$result = "echo "stuf"; include("thatfile.php");";

many thx
Was This Post Helpful? 0
  • +
  • -

#4 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Php In Mysql

Posted 14 December 2004 - 04:03 PM

Nope, if you have your query:
<?
$sql = "SELECT code FROM templates;";

// execute SQL query and get result
$sql_result = mysql_query($sql,$connection)
    or die(mysql_error()); 
?>


And you echo it out like this:
<?
// Loop through the data set and extract each row in to it's own variable set

while ($row = mysql_fetch_array($sql_result)) {
    extract($row);

//Evaluate the code that was retrieved from the database, any code in the result will be executed as PHP
eval($code);

// End while loop
}
?>


So in your database, you could put ANY php code you want and it will be executed when you pull it back on your page.

If this isn't what you are trying to do, lemme know and I'll try to understand what you need to accomplish.
Was This Post Helpful? 0
  • +
  • -

#5 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Re: Php In Mysql

Posted 14 December 2004 - 06:06 PM

awesome, got it.
many thx
Was This Post Helpful? 0
  • +
  • -

#6 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1876
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Re: Php In Mysql

Posted 14 December 2004 - 08:34 PM

Excellent, glad we were able to help!
Was This Post Helpful? 0
  • +
  • -

#7 cyberscribe  Icon User is offline

  • humble.genius
  • member icon

Reputation: 10
  • View blog
  • Posts: 1,062
  • Joined: 05-May 02

Re: Php In Mysql

Posted 15 December 2004 - 12:06 AM

One other small point on this topic -- if you just want to store data structures (like arrays) you can use serialize() to put the structure into a string format and unserialize() to return it to a variable. A bit safer than eval(), which can have security consequences if you ever include user-modifiable input in the statement to be eval()'d.

Also, consider enclosing the string to be eval'd in {} instead of using eval() for simple variable evaluations, etc. It's much faster.
Was This Post Helpful? 0
  • +
  • -

#8 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Re: Php In Mysql

Posted 15 December 2004 - 10:42 AM

cyberscribe, on Dec 15 2004, 12:06 AM, said:

One other small point on this topic -- if you just want to store data structures (like arrays) you can use serialize() to put the structure into a string format and unserialize() to return it to a variable. A bit safer than eval(), which can have security consequences if you ever include user-modifiable input in the statement to be eval()'d.

Also, consider enclosing the string to be eval'd in {} instead of using eval() for simple variable evaluations, etc. It's much faster.

not completely understand.
get the idea, but still a bit of a novice to php.
i do best with an example.
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Php In Mysql

Posted 17 December 2004 - 10:24 AM

cyberscribe, on Dec 15 2004, 02:06 AM, said:

One other small point on this topic -- if you just want to store data structures (like arrays) you can use serialize() to put the structure into a string format and unserialize() to return it to a variable. A bit safer than eval(), which can have security consequences if you ever include user-modifiable input in the statement to be eval()'d.

Cyberscribe, what's your take on this? I know you work with PHP a great deal, I'd be interested to hear your opinion. Seems to me you'd be able to avoid most problems by validating user input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1