SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to echo row in database with variable

    I have a table in a database that has content and a php code.

    PHP Code:
    Hello my name is <?php echo $var?>.
    Whenever I echo the content from the table it does not execute the PHP code. It just displays the code.

    Any advice on how I can get the PHP to execute.

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,079
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wickedneat View Post
    I have a table in a database that has content and a php code.

    PHP Code:
    Hello my name is <?php echo $var?>.
    Whenever I echo the content from the table it does not execute the PHP code. It just displays the code.

    Any advice on how I can get the PHP to execute.
    To run a string as php code, use eval();

    But why on earth would you be doing this?

  3. #3
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have previously tried using eval but I get the following error:

    Parse error: syntax error, unexpected '<' in hello.php(58) : eval()'d code on line 1

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,079
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    It will execute the entire string... "Hello my name is" is not valid php. You'd have to find Php tags, pull them out of the string and execute only that (after removing the php tags)

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    It will execute the entire string... "Hello my name is" is not valid php. You'd have to find Php tags, pull them out of the string and execute only that (after removing the php tags)

    This works:

    PHP Code:
    <?php
    $var 
    'omg';
    $variable 'Hello my name is <?php echo $var; ?>.';
    preg_match_all('/<?php (.*?) \?>/ms'$variable$match);
    eval(
    reset($match[1])); 

    ?>

    Curious, what's your use?

  6. #6
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Are you trying to print the content from db.If yes,then this code can help you:

    PHP Code:
    <?php
    mysql_connect
    ("hostname""username""password") && mysql_select_db("dbname") or die;
    $details mysql_query("select * from tablename");
    while (
    $detail mysql_fetch_assoc($details))
            {
            
    $name $detail['name'];
            echo 
    "$name";
            }
    ?>
    Thanks!


Bookmarks

Posting Permissions

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