SoapLady Posted October 27, 2006 Share Posted October 27, 2006 I have a MySQL table with various email messages I want to send out. (Want to do it that way so they can be edited in our Admin area).Example: Table: Email FieldName: Email_content[color=red]<p>Dear $firstname,</p><p>We received a request to send you your login information. It is as follows:</p><p>username: <b>$username</b></p><p>password: <b>$password</b></p>[/color]Once I get the data (via SELECT query), I can access it fine, but can't get the variables replaced by the values (which come from $_SESSION array).This DOES NOT work; the text with $username and $password unreplaced shows up in the email.[code]<snipped from inside class>[SELECT QUERY]$row = mysql_fetch_array($result)$this->mailText = $row[email_content];function __construct($firstname, $username, $password){ $this->htmlContent = "$this->mailText";}// $this->htmlContent is then passed to the email sending function<end snippet>[/code]However, if I do the exact same thing but put the TEXT in the function, the variables are replaced and the email is correct:[code]function __construct($firstname, $username, $password){$this->htmlContent = "<p>Dear $firstname,</p><p>We received a request to send you your login information. It is as follows:</p><p>username: <b>$username</b></p><p>password: <b>$password</b></p>";}[/code]I'm sure there is something fairly simple I need to do, I just can't figure out what it is!Help would be appreciated.SoapLady Link to comment https://forums.phpfreaks.com/topic/25258-getting-values-into-variables-in-mysql-result-field/ Share on other sites More sharing options...
bqallover Posted October 27, 2006 Share Posted October 27, 2006 Hmmm. It seems variable interpolation isn't being carried out on your returned database text. The following should work as a workaround, though it's untested. Obviously you can extend the arrays to include other variables you need.[code]function __construct($firstname, $username, $password){$find = array('$firstname', '$username', '$password' );$replace = array( $firstname, $username, $password );$this->htmlContent = str_replace( $find, $replace, $this->mailText );}[/code]Hope that helps! :) Link to comment https://forums.phpfreaks.com/topic/25258-getting-values-into-variables-in-mysql-result-field/#findComment-115186 Share on other sites More sharing options...
SoapLady Posted October 27, 2006 Author Share Posted October 27, 2006 It does seem like that would work ... but it doesn't!I still get the email with $username in the text, rather than the value assigned in the function.Any other ideas?SoapLady Link to comment https://forums.phpfreaks.com/topic/25258-getting-values-into-variables-in-mysql-result-field/#findComment-115214 Share on other sites More sharing options...
bqallover Posted October 29, 2006 Share Posted October 29, 2006 Maybe try something using eval? I'm not sure how this will work in your situation, but something like...[code]function __construct($firstname, $username, $password){$php_code = '$this->htmlContent = "' . $this->mailText . '";';eval( $php_code );}[/code]$this->htmlContent should now be correct? Well, it MIGHT be. ;) Link to comment https://forums.phpfreaks.com/topic/25258-getting-values-into-variables-in-mysql-result-field/#findComment-116133 Share on other sites More sharing options...
Barand Posted October 29, 2006 Share Posted October 29, 2006 or[code]<?phpsession_start();$msg = '<p>Dear $firstname,</p><p>We received a request to send you your login information. It is as follows:</p><p>username: $username</p><p>password: $password</p>';$s = array('$firstname', '$username', '$password');$r = array($_SESSION['firstname'], $_SESSION['username'], $_SESSION['password']);$msg = str_replace ($s, $r, $msg);echo $msg;?>[/code] Link to comment https://forums.phpfreaks.com/topic/25258-getting-values-into-variables-in-mysql-result-field/#findComment-116220 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.