Solved

Use variable stored in mysql database

Posted on 2013-11-14
8
290 Views
Last Modified: 2013-11-15
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.

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;

Open in new window


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
0
Comment
Question by:stkoontz
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39649565
This seems to be a templating issue.  Try this function:
http://php.net/manual/en/function.str-replace.php
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39649582
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.

echo $var_lname.' '.$var_email_text;

Open in new window

0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39649724
You could potential store your own version of a variable in the database, and then when you pull out the record, do a str_replace or regex on it.

Another alternative, but probably less robust is to store it in a format that printf would understand. Store this:

%s, thanks for registering for Momentum 2014.

And then when you pull it out of the database, output it like this:

$var_lname="Steve";
printf($var_email_text, $var_lname);

Open in new window

Using the printf function will replace the %s with the value stored in $var_lname
0
 
LVL 2

Expert Comment

by:loki0609
ID: 39650502
What your looking for is the eval function.

http://www.php.net/manual/en/function.eval.php
0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39650620
@loki0609 - read the first line of that page:

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!!
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39650931
To quote Rasmus: If eval() is the answer, you're almost certainly asking the wrong question!
0
 
LVL 2

Author Closing Comment

by:stkoontz
ID: 39651190
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_email_text, $var_fname)

Thanks!

Steve
0
 
LVL 2

Expert Comment

by:loki0609
ID: 39651593
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
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now