stkoontz
asked on
Use variable stored in mysql database
I'd like to store a variable inside a mysql database. Then assign that variable a value after it's pulled from the database.
Here's a very basic example of how I'm storing the info in the database.
**********************
echo "name" . $var_lname; Thanks for registering for Momentum 2014. **********************
I can pull the text from the database, but $var_fname comes out as plain text and not the value assigned to the variable in the code.
Here's example code.
Instead of replacing the variable $var_lname with "Steve" I get the exact text that's in the database...
**********************
echo "name" . $var_lname; Thanks for registering for Momentum 2014.
**********************
Any ideas on how to tell PHP that $var_lname is a variable?
Thanks,
Steve
Here's a very basic example of how I'm storing the info in the database.
**********************
echo "name" . $var_lname; Thanks for registering for Momentum 2014. **********************
I can pull the text from the database, but $var_fname comes out as plain text and not the value assigned to the variable in the code.
Here's example code.
try {
$get_email_text = new PDO("database information");
foreach($get_email_text->query("Query here") as $row) {
$var_email_text = $row['email_text'];
}
$get_email_text = null;
} catch (PDOException $e) {
print "Error!";
die();
}
$var_lname="Steve";
echo $var_email_text;
Instead of replacing the variable $var_lname with "Steve" I get the exact text that's in the database...
**********************
echo "name" . $var_lname; Thanks for registering for Momentum 2014.
**********************
Any ideas on how to tell PHP that $var_lname is a variable?
Thanks,
Steve
If you included $var_lname; in the text in the database, the answer is no, it is Not a PHP variable, it is just text. Your code is doing exactly what it is supposed to do.
Variables must be in the PHP source code, not in the database. When a PHP page is requested, the first thing that happens is the PHP interpreter "pre-compiles" the source code including the variables. Since the database content is not part of that, it does not get "pre-compiled" into the program. You can assign data fetched from the database into PHP variables that have already been defined.
Try this in place of line 13 to see what happens.
Variables must be in the PHP source code, not in the database. When a PHP page is requested, the first thing that happens is the PHP interpreter "pre-compiles" the source code including the variables. Since the database content is not part of that, it does not get "pre-compiled" into the program. You can assign data fetched from the database into PHP variables that have already been defined.
Try this in place of line 13 to see what happens.
echo $var_lname.' '.$var_email_text;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@loki0609 - read the first line of that page:
Storing PHP code in a database and then running eval() on it is a nightmare waiting to happen - Don't do it!!
The eval() language construct is very dangerous because it allows execution of arbitrary PHP code.
Storing PHP code in a database and then running eval() on it is a nightmare waiting to happen - Don't do it!!
To quote Rasmus: If eval() is the answer, you're almost certainly asking the wrong question!
ASKER
Using %s in the database so printf understands it works great. I tweaked it a bit and found sprintf to store the string into a variable so I can email it out.
$var_text=sprintf($var_ema il_text, $var_fname)
Thanks!
Steve
$var_text=sprintf($var_ema
Thanks!
Steve
eval is completely fine to use in the right circumstances. If the input is controlled and trusted, like a database of "templates" that you created that don't have output from outside then it's fine. It's used quite a bit in templating systems
http://php.net/manual/en/function.str-replace.php