Results 1 to 7 of 7

Thread: [RESOLVED] PHP inside a MySQL text field... Can it be done?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    Resolved [RESOLVED] PHP inside a MySQL text field... Can it be done?

    Hello all.
    Just wondering if there is a way of inserting PHP code in a database field, VARCHAR, MEDIUMTEXT, whatever, and have it echo as PHP code?
    I have tried:

    Code:
    $GLOBALS['txt'][0]
    and
    Code:
    <? echo $GLOBALS['txt'][0]; ?>
    But neither works.
    Any help is appreciated.
    Many thanks.
    Last edited by solitario; Apr 5th, 2006 at 12:26 AM.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: PHP inside a MySQL text field... Can it be done?

    Use eval() or output it to a temporary file and include() that file.

    Method 1:
    PHP Code:
    eval($GLOBALS['txt'][0]); 
    Method 2:
    PHP Code:
    file_put_contents('_temp.php'$GLOBALS['txt'][0]);
    include(
    '_temp.php']);
    unlink('_temp.php'); 
    Should you use the second method be sure that the PHP code has <?php ... ?> tags around it.
    Last edited by penagate; Apr 3rd, 2006 at 11:51 PM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    Re: PHP inside a MySQL text field... Can it be done?

    Thanks penagate.
    Perhaps I'm not doing this right.

    Here is my query:
    Code:
    SELECT reservationist_name FROM reservationists WHERE reservationist_id=0;
    Here is how I'm looping through the recordset:
    Code:
    while($row=mysql_fetch_object($sql)) {
    	$reservationist_name=$row->reservationist_name
    }
    Yet when I echo it to the page via "echo $reservationist_name;" it's giving me "eval($GLOBALS['txt'][0]);" as text. (The $txt array is right on the page, so I know it's there.) Anyone got an idea why it's not showing me the value of the variable? For the record it is:
    Code:
    $txt[0]='Reservationist Not Found';
    I'd just hate to write that tiny string to a temporary file first, but I will if I have to.
    Many thanks.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: PHP inside a MySQL text field... Can it be done?

    I'm confused. Where is the PHP code stored in the databse?

    eval(), will execute PHP code passed to it as a string - if you have actual PHP code in a database field then when you retrieve that field value you pass it to eval().

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: PHP inside a MySQL text field... Can it be done?

    Why can't you just echo $txt[0];

    ?

    $reservationist_name = "echo $_GLOBALS['txt'][o];";
    eval($reservantionist_name);

  6. #6
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: PHP inside a MySQL text field... Can it be done?

    Using eval can lead to big security problems, make sure that a user cannot manipulate what is going to be executed. You wouldn't want code to be allowed to be executed which would allow the user to drop all the tables in your database.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    Re: PHP inside a MySQL text field... Can it be done?

    Quote Originally Posted by penagate
    ...then when you retrieve that field value you pass it to eval().
    Ah, that did it. Thanks!
    Quote Originally Posted by da_silvy
    Why can't you just echo $txt[0];
    I would have, but I'm structuring my data so that 0 is a "catch all":
    Code:
    0	$GLOBALS['txt'][0]
    1	Tom
    2	Dick
    3	Harry
    The $txt array is going into an external language file.

    And thanks, john tindell. I didn't know that.

    Thanks folks. This one is resolved.

Posting Permissions

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



Click Here to Expand Forum to Full Width