www.webdeveloper.com
Results 1 to 4 of 4

Thread: Variable in database field (variable in variable)

  1. #1
    Join Date
    Jan 2008
    Posts
    10

    Variable in database field (variable in variable)

    I have a variable defined in the php page code, called $id.

    I am also displaying a database field from a mysql database, called $text.

    The $text database field needs to display and load the $id variable in it as well.

    Example:

    $id='1111'

    $result=mysql_query("SELECT text FROM testtable WHERE id='1');
    $text=mysql_result($resultlink1,$i,"text");

    echo $text;


    What does the $text field need to contain when I insert it into the testtable to display the $id in it as well.

    Say the data in $text field is: 'here is the text and the id is $id'

    What code needs to go around the $id part to display properly?

    I guess it is a variable in a variable?

  2. #2
    Join Date
    May 2003
    Location
    Milwaukee
    Posts
    347
    echo "here is the text and the id is $id";

    or

    echo "here is the text and the id is " . $id;

    for inserting, change echo to a variable name.

    $newtext = "here is the text and the id is $id";

  3. #3
    Join Date
    Feb 2003
    Location
    Derby, UK
    Posts
    456
    There are two basic ways to go about this - either use eval e.g.

    eval('echo("'.$text.'");')

    or if like me you think eval is the spawn of satan use some form of regex replacement. If you literally just want variables $xxxxx then something like

    $newtext = preg_replace('/\$(\w+)/e','$$1',$text);

    If you want parsing of fuller PHP code then eval may be the better (still evil) way to go.

    HTH,

    Dai

  4. #4
    Join Date
    Jan 2008
    Posts
    10
    Thank you both for your help. DailWelsh, your eval method worked perfectly. You Rock!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles