View previous topic :: View next topic |
Author |
Message |
olly86 -
Joined: 25 Apr 2003 Posts: 993 Location: Wiltshire, UK
|
Posted: Sat Nov 20, 2004 3:44 pm Post subject: php variable in mysql |
|
|
How can I place a PHP variable in a MySQL field, and when the field is displayed on a web page, get php to display the variables value.
eg:
how it should look: site design copyright © 2004 olly86
where the variable should be: site design copyright © 2004 ".$name." _________________ Olly |
|
Back to top |
|
|
admin Site Admin
Joined: 03 Mar 2002 Posts: 1295
|
Posted: Wed Nov 24, 2004 11:32 am Post subject: Re: php variable in mysql |
|
|
olly86 wrote: | How can I place a PHP variable in a MySQL field, and when the field is displayed on a web page, get php to display the variables value.
eg:
how it should look: site design copyright © 2004 olly86
where the variable should be: site design copyright © 2004 ".$name." |
You can use the eval() PHP function.
For example if the string stored in the database is 'copyright © 2004 $name' and is in $data, the following code can do the trick:
Code: |
$string = "copyright © 2004 \$name";
$name="olly";
eval("\$result = $data");
echo $result;
|
Notice the use of \$ when $ is to be included as is and the use of $ when we want the variable substitution.
You can find more explanations in the eval() section in the PHP manual. |
|
Back to top |
|
|
olly86 -
Joined: 25 Apr 2003 Posts: 993 Location: Wiltshire, UK
|
Posted: Sun Nov 28, 2004 2:11 pm Post subject: Re: php variable in mysql |
|
|
admin wrote: | olly86 wrote: | How can I place a PHP variable in a MySQL field, and when the field is displayed on a web page, get php to display the variables value.
eg:
how it should look: site design copyright © 2004 olly86
where the variable should be: site design copyright © 2004 ".$name." |
You can use the eval() PHP function.
For example if the string stored in the database is 'copyright © 2004 $name' and is in $data, the following code can do the trick:
Code: |
$string = "copyright © 2004 \$name";
$name="olly";
eval("\$result = $data");
echo $result;
|
Notice the use of \$ when $ is to be included as is and the use of $ when we want the variable substitution.
You can find more explanations in the eval() section in the PHP manual. |
the code that you gave me doesn't work, it results in this error:
Quote: | Parse error: syntax error, unexpected T_STRING in E:\Servers\Abyss Web Server\htdocs\WoottonBassettScouts.org.uk\Home\test.php(20) : eval()'d code on line 1 |
line 20 is the eval function _________________ Olly |
|
Back to top |
|
|
roganty -
Joined: 08 Jun 2004 Posts: 357 Location: Bristol, UK
|
Posted: Sun Nov 28, 2004 3:33 pm Post subject: Re: php variable in mysql |
|
|
try changing the eval() line to the following:
Code: | $string = "copyright © 2004 \$name";
$name="olly";
eval("\$result = $string");
echo $result; |
_________________ Anthony R
Roganty | Links-Links.co.uk |
|
Back to top |
|
|
olly86 -
Joined: 25 Apr 2003 Posts: 993 Location: Wiltshire, UK
|
Posted: Sun Nov 28, 2004 4:01 pm Post subject: |
|
|
I now get this error message:
Quote: | Parse error: syntax error, unexpected T_STRING in E:\Servers\Abyss Web Server\htdocs\WoottonBassettScouts.org.uk\Home\test.php(20) : eval()'d code on line 1 |
_________________ Olly |
|
Back to top |
|
|
roganty -
Joined: 08 Jun 2004 Posts: 357 Location: Bristol, UK
|
Posted: Sun Nov 28, 2004 4:04 pm Post subject: Re: php variable in mysql |
|
|
Ok, getting closer!
try this, (swap the 1st 2 lines around):
Code: | $name="olly";
$string = "copyright © 2004 \$name";
eval("\$result = $string");
echo $result; |
_________________ Anthony R
Roganty | Links-Links.co.uk |
|
Back to top |
|
|
olly86 -
Joined: 25 Apr 2003 Posts: 993 Location: Wiltshire, UK
|
Posted: Sun Nov 28, 2004 4:48 pm Post subject: |
|
|
same error:
Quote: | Parse error: syntax error, unexpected T_STRING in E:\Servers\Abyss Web Server\htdocs\WoottonBassettScouts.org.uk\Home\test.php(19) : eval()'d code on line 1 |
_________________ Olly |
|
Back to top |
|
|
roganty -
Joined: 08 Jun 2004 Posts: 357 Location: Bristol, UK
|
Posted: Mon Nov 29, 2004 10:34 am Post subject: Re: php variable in mysql |
|
|
Ok, its the same line, same error
try this:
Code: | $name="olly";
$string = "copyright © 2004 \$name";
eval("\$result = " .$string);
echo $result; |
or if that don't work try this:
Code: | $name="olly";
$string = "copyright © 2004 \$name";
eval("\$result = \"" .$string. "\"");
echo $result; |
_________________ Anthony R
Roganty | Links-Links.co.uk |
|
Back to top |
|
|
olly86 -
Joined: 25 Apr 2003 Posts: 993 Location: Wiltshire, UK
|
Posted: Mon Nov 29, 2004 6:42 pm Post subject: |
|
|
error from first code block:
Parse error: syntax error, unexpected T_STRING in E:\Servers\Abyss Web Server\htdocs\WoottonBassettScouts.org.uk\Home\test.php(21) : eval()'d code on line 1
error from second code block:
Parse error: syntax error, unexpected $end in E:\Servers\Abyss Web Server\htdocs\WoottonBassettScouts.org.uk\Home\test.php(21) : eval()'d code on line 1 _________________ Olly |
|
Back to top |
|
|
roganty -
Joined: 08 Jun 2004 Posts: 357 Location: Bristol, UK
|
Posted: Mon Nov 29, 2004 9:27 pm Post subject: Re: php variable in mysql |
|
|
php.net wrote: | eval() evaluates the string given in code_str as PHP code. Among other things, this can be useful for storing code in a database text field for later execution.
There are some factors to keep in mind when using eval(). Remember that the string passed must be valid PHP code, including things like terminating statements with a semicolon so the parser doesn't die on the line after the eval(), and properly escaping things in code_str.
Also remember that variables given values under eval() will retain these values in the main script afterwards.
Example 1. eval() example - simple text merge
Code: | <?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
?> |
|
According to the above, this should work:
Code: | eval("\$result = \"$string\";"); |
_________________ Anthony R
Roganty | Links-Links.co.uk |
|
Back to top |
|
|
olly86 -
Joined: 25 Apr 2003 Posts: 993 Location: Wiltshire, UK
|
Posted: Thu Dec 02, 2004 9:21 pm Post subject: |
|
|
thanks that's working great now, for some reason when I used that code before it didn't work. _________________ Olly |
|
Back to top |
|
|
|