SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Sep 2003
    0 Post(s)
    0 Thread(s)

    Problem when using variable names from a db field

    OK, this isn't the easiest problem to explain, but I'll try

    A bit of background - This script is used to email a user a new password if they follow a 'forgotten password' link on my site.

    In one of the pages, I have the following, to get information about the user from the database:
    $pw_rs = mysql_query("SELECT id, firstname, username, email FROM tsd_members WHERE username='$username'");
    $pw_row = mysql_fetch_array($pw_rs);
    Then I need to email the user a new password. The email which is sent out needs to be content manageable, so its stored in the database. I get the neccesary information from the db using this query:
    $automail_rs = mysql_query("SELECT * FROM tsd_automails WHERE id='1'");
    $automail_row = mysql_fetch_array($automail_rs);
    The problem occurs because the 'body' field of the 'tsd_automails' table needs to use variables which were defined in the '$pw_row' array, for example, the 'forgotten password' body field includes:
    Hi $pw_row[firstname] (ID $pw_row[id])...

    Instead of using the variables defined in the first query, the variable names are printed (i'm using echo $automail_row[body] for testing), ie it just outputs exactly whats in the db field.
    Any ideas how I can make it use the actual variable rather than just outputting the exact contents of the field.

    Hopefully that makes enough sense

  2. #2
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Dog Street
    1 Post(s)
    1 Thread(s)
    Instead of storing the actual variable names you could store place holders
    and parse them into real values.

    PHP Code:


    $name 'King Edward';

    $string 'This is my email body.  Hi {VAR_NAME}, welcome to my email body!';

    $string preg_replace("/$placeHolder/"$name$string);


    output: This is my email body. Hi King Edward, welcome to my email body!

    If you wanted to parse a bunch of these place holders you could put
    them in an array.

    PHP Code:

    'King Edward';

    $pass 'pinkElephantxxx';

    $string 'This is my email body.  Hi {VAR_NAME}, welcome to my email body! Your new password is: {VAR_NEWPASS}.';

    $placeHolderArr = array('{VAR_NAME}'=>$name'{VAR_NEWPASS}'=>$pass);

    foreach (
    $placeHolderArr as $key=>$val)
    $string preg_replace("/$key/"$val$string);


    This is my email body. Hi King Edward, welcome to my email body! Your new password is: pinkElephantxxx.

    This is just one possible way to do it. Not necessarily the best.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts