[PHP] Re: Eval var from query
eval($data) returns Parse error: parse error, unexpected T_STRING in C:\apps\apache2\htdocs\test\query.php(23) : eval()'d code on line 1 Thanks! Shawn Shawn McKenzie [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] How can I evaluate a var that is from a text field of a database? Example: MySQL field `name` = hi my name is $name In my script I have: $name = Shawn; After fetching a query result as an associative array I have the contents of the `name` field in $data If I echo $data I get: hi my name is $name I would like to get: hi my name is Shawn TIA, Shawn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Eval var from query
The string you send to eval() must be valid PHP code. So try this.. eval( 'echo '.$data.';'); - Kevin - Original Message - From: Shawn McKenzie [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, July 14, 2003 1:15 PM Subject: [PHP] Re: Eval var from query eval($data) returns Parse error: parse error, unexpected T_STRING in C:\apps\apache2\htdocs\test\query.php(23) : eval()'d code on line 1 Thanks! Shawn Shawn McKenzie [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] How can I evaluate a var that is from a text field of a database? Example: MySQL field `name` = hi my name is $name In my script I have: $name = Shawn; After fetching a query result as an associative array I have the contents of the `name` field in $data If I echo $data I get: hi my name is $name I would like to get: hi my name is Shawn TIA, Shawn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Eval var from query
I realized that may not make any sense. Following up the eample relates to your original query like this.. MySQL field `name` = hi my name is $name In my script I have: $name = Shawn; $data = 'hi my name is $name'; $code = 'echo '.$data.';'; $name = 'Shawn'; eval($code); // prints hi may name is Shawn. Hope that makes it more clear. - Kevin - Original Message - From: Kevin Stone [EMAIL PROTECTED] To: PHP-GENERAL [EMAIL PROTECTED] Sent: Monday, July 14, 2003 1:23 PM Subject: Re: [PHP] Re: Eval var from query The string you send to eval() must be valid PHP code. So try this.. eval( 'echo '.$data.';'); - Kevin - Original Message - From: Shawn McKenzie [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, July 14, 2003 1:15 PM Subject: [PHP] Re: Eval var from query eval($data) returns Parse error: parse error, unexpected T_STRING in C:\apps\apache2\htdocs\test\query.php(23) : eval()'d code on line 1 Thanks! Shawn Shawn McKenzie [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] How can I evaluate a var that is from a text field of a database? Example: MySQL field `name` = hi my name is $name In my script I have: $name = Shawn; After fetching a query result as an associative array I have the contents of the `name` field in $data If I echo $data I get: hi my name is $name I would like to get: hi my name is Shawn TIA, Shawn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Eval var from query
Thanks Kevin! That works great. It outputs: hi my name is Shawn Now if I want to assign $data to another var, let's say $newdata and have it eval the $name var inside of that. How would that work? Meaning I want to $newdata = hi my name is Shawn Thanks! Shawn Kevin Stone [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] The string you send to eval() must be valid PHP code. So try this.. eval( 'echo '.$data.';'); - Kevin - Original Message - From: Shawn McKenzie [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, July 14, 2003 1:15 PM Subject: [PHP] Re: Eval var from query eval($data) returns Parse error: parse error, unexpected T_STRING in C:\apps\apache2\htdocs\test\query.php(23) : eval()'d code on line 1 Thanks! Shawn Shawn McKenzie [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] How can I evaluate a var that is from a text field of a database? Example: MySQL field `name` = hi my name is $name In my script I have: $name = Shawn; After fetching a query result as an associative array I have the contents of the `name` field in $data If I echo $data I get: hi my name is $name I would like to get: hi my name is Shawn TIA, Shawn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Eval var from query
Got it! eval( '$newdata = '.$data.';'); Thanks! Shawn Shawn McKenzie [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Thanks Kevin! That works great. It outputs: hi my name is Shawn Now if I want to assign $data to another var, let's say $newdata and have it eval the $name var inside of that. How would that work? Meaning I want to $newdata = hi my name is Shawn Thanks! Shawn Kevin Stone [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] The string you send to eval() must be valid PHP code. So try this.. eval( 'echo '.$data.';'); - Kevin - Original Message - From: Shawn McKenzie [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, July 14, 2003 1:15 PM Subject: [PHP] Re: Eval var from query eval($data) returns Parse error: parse error, unexpected T_STRING in C:\apps\apache2\htdocs\test\query.php(23) : eval()'d code on line 1 Thanks! Shawn Shawn McKenzie [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] How can I evaluate a var that is from a text field of a database? Example: MySQL field `name` = hi my name is $name In my script I have: $name = Shawn; After fetching a query result as an associative array I have the contents of the `name` field in $data If I echo $data I get: hi my name is $name I would like to get: hi my name is Shawn TIA, Shawn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Eval var from query
- Original Message - From: Shawn McKenzie [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, July 14, 2003 1:51 PM Subject: Re: [PHP] Re: Eval var from query Thanks Kevin! That works great. It outputs: hi my name is Shawn Now if I want to assign $data to another var, let's say $newdata and have it eval the $name var inside of that. How would that work? Meaning I want to $newdata = hi my name is Shawn Thanks! Shawn Umm.. well.. '$name' is a litteral inside the string until it is evaluated. It doesn't matter if you make a copy of the variable, '$name' is still going to be a litteral value. So you'll have to eval() it exactly the same way every time. But maybe I can offer an alternative... You look at this string and see a code evaluation problem. I look at this string and see a find and replace problem. Consider useing str_replace() to search for and replace '$name' as a template marker rather than a variable to be evaluated.. $data = 'hi my name is $name'; $name = 'Shawn'; echo str_replace('$name', $name, $data); This accomplishes exactly the same thing and it's a perfectly valid form. Plus it's going to work on any string becuase you don't have to turn them into PHP code first. - Kevin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php