Jump to content


Photo

Getting values into variables in MySQL result field


  • Please log in to reply
4 replies to this topic

#1 SoapLady

SoapLady
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 27 October 2006 - 01:44 AM

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

<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>


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.
<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>

However, if I do the exact same thing but put the TEXT in the function, the variables are replaced and the email is correct:

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>";
}

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


#2 bqallover

bqallover
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts
  • LocationLeeds, UK

Posted 27 October 2006 - 02:12 AM

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.

function __construct($firstname, $username, $password)
{
$find = array('$firstname', '$username', '$password' );
$replace = array( $firstname, $username, $password );

$this->htmlContent = str_replace( $find, $replace, $this->mailText );
}

Hope that helps! :)

#3 SoapLady

SoapLady
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 27 October 2006 - 03:23 AM

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

#4 bqallover

bqallover
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts
  • LocationLeeds, UK

Posted 29 October 2006 - 02:51 AM

Maybe try something using eval?  I'm not sure how this will work in your situation, but something like...

function __construct($firstname, $username, $password)
{
$php_code = '$this->htmlContent = "' . $this->mailText . '";';
eval( $php_code );
}

$this->htmlContent should now be correct?  Well, it MIGHT be.  ;)

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 29 October 2006 - 11:22 AM

or
<?php
session_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;
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users