SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Zealot dcgamers's Avatar
    Join Date
    Dec 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Holding a variable in an array from a mysql database

    This is very hard to explain but I have a piece of code that gets a row from a mysql table and displays it. But what if what I'm displaying contains a variable, how do I make the variable work instead of just having it print the variable name?

    My code:
    PHP Code:
    $query mysql_query("SELECT * FROM templates WHERE tid='$defaulttheme' AND name='$fieldtodisplay'");
    $result mysql_fetch_array($query);
    $body $result[body]; 
    Get what I'm saying?

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use eval().

  3. #3
    SitePoint Zealot dcgamers's Avatar
    Join Date
    Dec 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did but it returned:
    Parse error: syntax error, unexpected T_LNUMBER in /URLREMOVED/config.php(25) : eval()'d code on line 2

    Im not sure I'm using it right:
    PHP Code:
    $body = eval("\$result[body] = \"$result[body]\";"); 

  4. #4
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming the variable's name is in $row['body'], doing the following should echo the variable's value:
    PHP Code:
    echo ${$row['body']}; 
    PHP questions? RTFM
    MySQL questions? RTFM

  5. #5
    SitePoint Zealot dcgamers's Avatar
    Join Date
    Dec 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @kromey:
    that did nothing.

    Does it matter that I am doing this in a function?

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does $row['body'] contain?

  7. #7
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it matters that you are doing it inside a function, if the record set was created outside of the function and you have not passed the record set or row to the function then the variable is out of scope.
    You do realize you need quotes around the field name : $row['body'] ?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  8. #8
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    try eval('echo "' . $row['body'] . '"');

    however, i STRONGLY recommend that you reconsider the way you are doing this, especially if $row['body'] would ever contain user-supplied information.

  9. #9
    SitePoint Zealot dcgamers's Avatar
    Join Date
    Dec 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by longneck View Post
    try eval('echo "' . $row['body'] . '"');

    however, i STRONGLY recommend that you reconsider the way you are doing this, especially if $row['body'] would ever contain user-supplied information.
    I put that in where I display the body and I got this:
    Parse error: syntax error, unexpected $end, expecting ',' or ';' in /edited_out_url/config.php(49) : eval()'d code on line 1

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    try eval('echo "' . $row['body'] . '" ; ');

    And that?

  11. #11
    SitePoint Zealot dcgamers's Avatar
    Join Date
    Dec 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get:
    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in

  12. #12
    SitePoint Zealot dcgamers's Avatar
    Join Date
    Dec 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok thats fixed but now another problem. This is all being done in a function. In short, how do I make a global variable out of only the variables being printed in $body?

  13. #13
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suggest you ditch eval (which is extremely dangerous, especially if there is any user input at all in $body), and instead take an approach I did when I needed to be able to echo PHP variables inside user-submitted content in my own CMS:
    Code PHP:
    //replace template variables
    preg_match_all("/{:.*?:}/", $body, $matches);
    $vars = array_unique($matches[0]);
    foreach($vars as $find)
    {
    	$var = trim($find, "{:}");
    	if(strpos($var, "|"))
    	{
    		list($var,$i) = explode("|", $var,2);
    		if(isset(${$var}[$i])) //only replace values if the variable is defined
    			$content = str_replace($find, ${$var}[$i], $content);
    	} else {
    		if(isset($$var)) //only replace values if the variable is defined
    			$content = str_replace($find, $$var, $content);
    	}
    }
    To use this code, all you need to do is, in your body content, write your variables like so: {:varname:} That will equate to the variable $varname in your PHP code. If you need to access an array element, do it like so: {:arrayname|index:} Which will reference $arrayname['index']. (Note that this code does not support multi-dimensional arrays, but could be easily modified to do so.)

    To do this in a function, add the line
    PHP Code:
    global $$var
    before you check if it's defined; however, doing this may break the isset check, I haven't tested this.

    Doing this is a whole lot safer than allowing arbitrary user-supplied data to be executed as PHP on your server.
    PHP questions? RTFM
    MySQL questions? RTFM

  14. #14
    SitePoint Zealot dcgamers's Avatar
    Join Date
    Dec 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like your code but where exactly do I put it? When I print the content do I just have to do echo $content; ? Also, where exactly do I put global $$var, I am doing this in a function.

    UPDATE:
    I put in the code and after it, I put echo $content. But it displays nothing. Still don't know where to put global $$var though.

  15. #15
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops, sorry, I was modifying my code to match your variable and missed those - replace all instances of $content with your variable $body. This code would go where you have the eval statement (replace the eval line with this code).

    The global $$var line goes immediately before each if(isset(... line.
    PHP questions? RTFM
    MySQL questions? RTFM

  16. #16
    SitePoint Zealot dcgamers's Avatar
    Join Date
    Dec 2005
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Odd, it works but only the first variable is actually used, the others are just text. All of them are the same, as far as written as {:blah:} and {:blah|index:}

    Any thoughts?

  17. #17
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please post your current code, the entire function in question. Also, are you certain the variables actually exist? My code will only replace variables that exist.
    PHP questions? RTFM
    MySQL questions? RTFM


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
  •