Thread: double variable

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2000
    Location
    Hillsboro, OR, USA
    Posts
    12
    Rep Power
    0
    Please help!

    I have a variable $main that is coming from a mysql database.
    $main = mysql_result($resultp,$p,"main");
    In the database main contains the variable $food
    $food is definded in the form as
    $food = 'apples';

    Then when I print the variable $main it prints $food instead of apples.

    PRINT "<CENTER><P>results = $main</CENTER>";


    Does anyone know how to make this work?

    Thanks

    ------------------
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    $$main ???
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2000
    Location
    Hillsboro, OR, USA
    Posts
    12
    Rep Power
    0
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by freddydoesphp:
    $$main ???[/quote]


    Thanks

    I just tried that. It prints $$food


  6. #4
  7. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    If you literally stored the string '$food' then that's what you'll get out. Check your insert statement. I imagine you did something like:

    mysql_query('insert into table (field) values ("$food")');

    Notice the outside quotes are single not double. If that's the case than PHP will not convert the string $food into it's represented value apples. The outermost quotes must be double or PHP won't interpret variables in the string.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2000
    Location
    Hillsboro, OR, USA
    Posts
    12
    Rep Power
    0
    That doesn't seem to work.
    What you described is when the variable is stored.

    I am trying to store a string that includes a variable into a database, but I do not want the variable filled in until it is retrieved in another form.
    I would like to store
    My favorite food is $food.

    The second form would then take that string and substitute the variable $food for whatever the current value is for $food.

    But instead it prints $food instead of apples or whatever the variable $food is set to in the second form.

    It is like i need to somehow tell the 2nd form that $food needs to be parsed?????

    Any suggestions?

    Thanks


    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by rod k:
    If you literally stored the string '$food' then that's what you'll get out. Check your insert statement. I imagine you did something like:

    mysql_query('insert into table (field) values ("$food")');

    Notice the outside quotes are single not double. If that's the case than PHP will not convert the string $food into it's represented value apples. The outermost quotes must be double or PHP won't interpret variables in the string.
    [/quote]



    ------------------
  10. #6
  11. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,642
    Rep Power
    4492
    I believe you're looking for the eval() function.

    From the manual:

    <?php
    $string = 'cup';
    $name = 'coffee';
    $str = 'This is a $string with my $name in it.<br>';
    echo $str;
    eval ("$str = "$str";");
    //evaluates to: $str = "$str";
    echo $str;
    ?>

    The above example will show:
    This is a $string with my $name in it.
    This is a cup with my coffee in it.
    http://www.php.net/manual/function.eval.php

    ---John Holmes...

    ------------------
    *************************************************************
    * The manual can probably answer 90% of your questions...
    *
    * PHP Manual. www.php.net/manual
    * MySQL Manual: www.mysql.com/documentation/mysql/bychapter
    *************************************************************
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2000
    Location
    Hillsboro, OR, USA
    Posts
    12
    Rep Power
    0
    I cut and pasted you example in my page and it worked fine, but when I substituted the string:
    'This is a $string with my $name in it.<br>

    with the variable $main, which is looked up from a Mysql database and contains:
    'This is a $string with my $name in it.<br>

    This is what I get:
    $mainThis is a $string with my $name in it.

    Here is the code:

    $string = 'cup';
    $name = 'coffee';
    $str = '$main';
    echo $str;
    eval ("$str = "$str";");
    //evaluates to: $str = "$str";
    echo $str;


    Thanks

    Tim

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by SepodatiCreations:
    I believe you're looking for the eval() function.

    From the manual:

    <?php
    $string = 'cup';
    $name = 'coffee';
    $str = 'This is a $string with my $name in it.<br>';
    echo $str;
    eval ("$str = "$str";");
    //evaluates to: $str = "$str";
    echo $str;
    ?>

    The above example will show:
    This is a $string with my $name in it.
    This is a cup with my coffee in it.
    http://www.php.net/manual/function.eval.php

    ---John Holmes...

    [/quote]

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    When you use eval the string that you eval() must contain proper PHP syntax, so fi this came from your database you could not eval() it!
    'My favorite food is $food'
    But if you had
    'print "My favorite food is $food";'
    in there you could eval it.

    Not sure if this what you are after but here goes:

    $main = 'print "My favorite food is $food";';
    $food = 'eggs';
    //This would print "My favorite food is eggs"
    print eval($main);

  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2000
    Location
    Hillsboro, OR, USA
    Posts
    12
    Rep Power
    0
    I got it going with everyones help. I just needed to use the eval comand twice!
    Thanks

    Tim

    [/B][/QUOTE]

Similar Threads

  1. A variable $_POST variable?
    By CLuver in forum PHP Development
    Replies: 0
    Last Post: February 11th, 2004, 12:55 AM
  2. How to read multiple line text file into a variable
    By gatewaycityca in forum Visual Basic Programming
    Replies: 4
    Last Post: January 29th, 2004, 12:26 PM
  3. Replies: 7
    Last Post: January 16th, 2004, 05:44 AM
  4. Replies: 1
    Last Post: November 24th, 2003, 01:24 AM
  5. Replies: 5
    Last Post: November 16th, 2003, 11:31 AM

IMN logo majestic logo threadwatch logo seochat tools logo