php variable in mysql

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
olly86
-


Joined: 25 Apr 2003
Posts: 993
Location: Wiltshire, UK

PostPosted: Sat Nov 20, 2004 3:44 pm    Post subject: php variable in mysql Reply with quote

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 View user's profile Send private message
admin
Site Admin


Joined: 03 Mar 2002
Posts: 1295

PostPosted: Wed Nov 24, 2004 11:32 am    Post subject: Re: php variable in mysql Reply with quote

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 View user's profile Send private message
olly86
-


Joined: 25 Apr 2003
Posts: 993
Location: Wiltshire, UK

PostPosted: Sun Nov 28, 2004 2:11 pm    Post subject: Re: php variable in mysql Reply with quote

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 View user's profile Send private message
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Sun Nov 28, 2004 3:33 pm    Post subject: Re: php variable in mysql Reply with quote

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 View user's profile Send private message Visit poster's website
olly86
-


Joined: 25 Apr 2003
Posts: 993
Location: Wiltshire, UK

PostPosted: Sun Nov 28, 2004 4:01 pm    Post subject: Reply with quote

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 View user's profile Send private message
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Sun Nov 28, 2004 4:04 pm    Post subject: Re: php variable in mysql Reply with quote

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 View user's profile Send private message Visit poster's website
olly86
-


Joined: 25 Apr 2003
Posts: 993
Location: Wiltshire, UK

PostPosted: Sun Nov 28, 2004 4:48 pm    Post subject: Reply with quote

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 View user's profile Send private message
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Mon Nov 29, 2004 10:34 am    Post subject: Re: php variable in mysql Reply with quote

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 View user's profile Send private message Visit poster's website
olly86
-


Joined: 25 Apr 2003
Posts: 993
Location: Wiltshire, UK

PostPosted: Mon Nov 29, 2004 6:42 pm    Post subject: Reply with quote

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 View user's profile Send private message
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Mon Nov 29, 2004 9:27 pm    Post subject: Re: php variable in mysql Reply with quote

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 View user's profile Send private message Visit poster's website
olly86
-


Joined: 25 Apr 2003
Posts: 993
Location: Wiltshire, UK

PostPosted: Thu Dec 02, 2004 9:21 pm    Post subject: Reply with quote

thanks that's working great now, for some reason when I used that code before it didn't work.
_________________
Olly
Back to top View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group