SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php and mysql database variables.

    Hello guys!!

    need help!!

    I am using php and mysql database. The spec is that all the text on the site comes from a database. This is fine untill i get to a bit of text which contains a variable or a session variable.

    if i store this sentence in my database and draw it back out again then the variable value is lost and only the variable name is echoed.

    ie. $database_data -> Hello $_SESSION['app_name'] thank you for registering!
    echo"$database_data";
    result->
    Hello $_SESSION['app_name'] thank you for registering!
    and not
    Hello James thank you for registering!
    I als have the same problem when i store emails which i need to send out in a database field with variables in it!!

    can anyone help please!!

    thanks

    james

  2. #2
    HardCoder md_irfan_amu's Avatar
    Join Date
    May 2005
    Location
    Asia
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually u r not saving waht you want to save,
    just check the variable before saving into databse wheather your correct value is coming in the variable
    Irfan
    Find Tutors Easyway to get connect with your tutor in india

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what do you mean?

    the code works fine if it is not put into the database. is there some special way of storing variables in a db?

  4. #4
    HardCoder md_irfan_amu's Avatar
    Join Date
    May 2005
    Location
    Asia
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just put the code u use in storing the database,
    Irfan
    Find Tutors Easyway to get connect with your tutor in india

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have tried that and as i said. the code works fine when it is not called up from the database. as sson as i put it into the db and call it back out then it doesnt work
    help please!!

  6. #6
    SitePoint Evangelist chiphunt1's Avatar
    Join Date
    Oct 2003
    Location
    louisville, ky
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Post the script (code) that you are working with, so we can see exactly what you're doing.

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok. so i have these variables. including session vars.

    for example $set_ref_yr below is caled from the database and given a number ie 7.

    now

    $db = @mysql_select_db( $database, $connection );

    $sql_st="SELECT * FROM `static_text` ORDER BY `id_st` ASC; ";

    $result_st = @mysql_query( $sql_st , $connection )
    or die( "Couldn't execute query: $sql_st;" . mysql_error() );

    while($row_st = mysql_fetch_array($result_st))
    {
    $set_ref_yr =stripslashes($row_st[st]);

    }


    now if I code
    <?php echo"You must have known this person for $set_ref_yr years or
    more "; ?>
    i get my desired results ie,
    You must have known this person for 7 years or more
    BUT.if i put the line
    "You must have known this person for $set_ref_yr years or
    more "
    in to the database, and call it back up

    $sql_st="SELECT * FROM `static_text` ORDER BY `id_st` ASC; ";

    $result_st = @mysql_query( $sql_st , $connection )
    or die( "Couldn't execute query: $sql_st;" . mysql_error() );

    while($row_st = mysql_fetch_array($result_st))
    {
    $text =stripslashes($row_st[text]);

    }

    and then echo"$text";
    then the out put is below

    You must have known this person for $set_ref_yr years or more .

    Is there something really simple i am missing?!? it just seems like the variable is not found and cannot be called, maybe some sort of notation prob?? i dunno! its driving me crazy! i get the same probs when i try and store emails with variable names in the db and send them out again!!

    help please!!
    thanks!

  8. #8
    SitePoint Evangelist chiphunt1's Avatar
    Join Date
    Oct 2003
    Location
    louisville, ky
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So when you look into your database, you see this line?

    You must have known this person for $set_ref_yr years or more .

    Can you post the script where you are doing the processing. I don't see where you are inserting the data into the database. Looks to me like it's adding slashes and the $ character is escaped because it's a special character. Then when select the data your are stripping the slashes, so it comes back out as $set_ref_yr instead of what the variable was originally set to.

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i actually dont have the code as such cos i have moved on and worked round the problem. but ideally i would like to solve it.

    I physically insert the variable $gap into the db myself not using php. I have tried it with slashes and without. As an example below give $gap a value and try and call it again using a db item.

    <?php
    $gap = "123456789";
    $db = @mysql_select_db( $database, $connection );

    $sql_st="
    SELECT * FROM `static_text` ORDER BY `id_st` ASC;
    ";

    $result_st = @mysql_query( $sql_st , $connection )
    or die( "Couldn't execute query: $sql_st;" . mysql_error() );

    while($row_st = mysql_fetch_array($result_st))
    {
    $db_variable =stripslashes($row_st[st]);

    }
    echo"$db_variable";

    //now if i echo $db_variable i want to see 123456789
    // but instead i get $gap outputted
    ?>

    where am i going wrong??

  10. #10
    SitePoint Zealot rain5017's Avatar
    Join Date
    Sep 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by james_l
    i actually dont have the code as such cos i have moved on and worked round the problem. but ideally i would like to solve it.

    I physically insert the variable $gap into the db myself not using php. I have tried it with slashes and without. As an example below give $gap a value and try and call it again using a db item.

    <?php
    $gap = "123456789";
    $db = @mysql_select_db( $database, $connection );

    $sql_st="
    SELECT * FROM `static_text` ORDER BY `id_st` ASC;
    ";

    $result_st = @mysql_query( $sql_st , $connection )
    or die( "Couldn't execute query: $sql_st;" . mysql_error() );

    while($row_st = mysql_fetch_array($result_st))
    {
    $db_variable =stripslashes($row_st[st]);

    }
    echo"$db_variable";

    //now if i echo $db_variable i want to see 123456789
    // but instead i get $gap outputted
    ?>

    where am i going wrong??

    $db_variable.=stripslashes($row_st[st]);
    ??

  11. #11
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the database retrieval part is fine, i just cut and pasted that out my script as an eg,
    my problem is the storing of the variable $gap in the db in a way that it can be retrieved and displayed as i want it to be

  12. #12
    SitePoint Wizard
    Join Date
    Jan 2004
    Location
    3rd rock from the sun
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try echoing or printing with single and then double quotes.

    Just a thought...

    PHP Code:
    $var="example";

    echo 
    'this is an $var';

    echo 
    'this is an '.$var'

    print ("this is an $var"); 
    PHP only parses double quoted strings for variables AFAIK

  13. #13
    SitePoint Evangelist chiphunt1's Avatar
    Join Date
    Oct 2003
    Location
    louisville, ky
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo "$db_variable"; // NO

    echo $db_variable; // YES

    SO:

    echo "this is the db variable : " . $db_variable . "that I want to print out";

    Don't put the variable itself in quotes. Just use the concat operator, which you know is the period and concat the string in quotes with the value of the variable.

  14. #14
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo "$db_variable"; // NO

    echo $db_variable; // NO

    none of the methods work. the concatenating fails also. is it something to do with the variable being stored in the db, and not being able to take the value of the variable before it is printed out?

    any more ideas would be gladly recieved

  15. #15
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post your insert query, b/c i agree that is where the problem seems to be occuring

  16. #16
    SitePoint Zealot rain5017's Avatar
    Join Date
    Sep 2005
    Posts
    166
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chiphunt1
    echo "$db_variable"; // NO

    echo $db_variable; // YES

    SO:

    echo "this is the db variable : " . $db_variable . "that I want to print out";

    Don't put the variable itself in quotes. Just use the concat operator, which you know is the period and concat the string in quotes with the value of the variable.
    echo "this is the db variable : " . $db_variable . "that I want to print out";//YES

    echo "this is the db variable : $db_variable that I want to print out";//YES

    echo "this is the db variable : ' $db_variable ' that I want to print out"; //NO

  17. #17
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont actually use an insert query doogle. i just put the text into the db using phpmyadmin,

  18. #18
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so in phpMyAdmin you put in something like 'Hello $_SESSION['username'] thank you for registering!' and then on the output it doesn't change $_SESSION['username'] to the persons username?

    If this is true they why not enter this into the database
    'Hello %s thank you for registering!'

    then on output
    PHP Code:
    echo printf($string_from_database,$_SESSION['username']); 

  19. #19
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks doogle that works a treat.

    but how do i apply that for multiple variables in the db field?
    ie database text: Hello %s %s thank you for registering - Being forename and surname.
    How do i print that out?

    also i am having problems with the same scenario but as email text. ie i put in a load of static text with variables in the db, when i call it i just want to assign it to a $body variable which i can use the mail() php function to send the email. any ideas?!?

    cheers!!

  20. #20
    SitePoint Wizard
    Join Date
    Jan 2004
    Location
    3rd rock from the sun
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    database text: Hello %s %s thank you for registering - Being forename and surname.
    If you are taking the trouble to store firstname, lastname and email in a session (and you call it registering) why not go the whole hog and store their registration in a database?

    This would mean in future you optionally :
    find out how many people registered
    how many emails you sent them
    create a mailing list
    etc

  21. #21
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by james_l
    Thanks doogle that works a treat.

    but how do i apply that for multiple variables in the db field?
    ie database text: Hello %s %s thank you for registering - Being forename and surname.
    How do i print that out?

    also i am having problems with the same scenario but as email text. ie i put in a load of static text with variables in the db, when i call it i just want to assign it to a $body variable which i can use the mail() php function to send the email. any ideas?!?

    cheers!!
    asd

  22. #22
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    help!

  23. #23
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you want to do more than one variable use the database text like paulyg posted

    database text: Hello %s %s thank you for registering

    where the first %s is the forename and the second %s is the surname
    then use a function like this
    PHP Code:
    echo printf($string_from_database,$_SESSION['forename'],$_SESSION['surname']); 
    it will replace the %s in order of the variables inside the printf() function. Add as many as you need

  24. #24
    SitePoint Member
    Join Date
    Aug 2005
    Location
    United Kingdom
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi James,

    I've encountered this very problem recently and came across this post. While this post didn't solve the issue we both seem to be having, I did find an answer elsewhere . . .

    So, you're storing the literal variable $fname in the database which is (perhaps) included in the body text of an e-mail (also stored in the database).

    When you want to send an e-mail to a customer (for example), you query the database and get the subject, the body and the customer details e.g. firstname, lastname, email etc. If I understand your problem correctly, when you try and send the body of the e-mail you get the following:

    Dear $firstname,

    Here is an e-mail for you.

    From James
    When of course what you want is:

    Dear Joe,

    Here is an e-mail for you.

    From James
    Correct? If so, you need to use PHP's eval() function. Here's a test I did that worked just fine:

    PHP Code:
    <?php

        
    // connect to database
        
    include ("config.php");
        
        
    // Get customer details from the database including name, email address etc
        
    $details_sql mysql_query("SELECT * FROM customers_details WHERE details_id='1'");
        
    $details_row mysql_fetch_array($details_sql);

        
    $name $details_row['details_fname'];
        
        
    // Get the details of the email including subject line, email body etc
        
    $send_sql mysql_query("SELECT * FROM customers_email WHERE email_id='3'");
        
    $send_row mysql_fetch_array($send_sql);

        
    // The body var contains a variable called $name (populated above) that we'll use
        // PHP's eval() function to translate when we print body of the e-mail to the browser
        // or for sending an e-mail in a PHP e-mail script
        
    $body $send_row['email_body'];
        


    ?>
    Now we use eval() with $body so we can use $name inside $body like this:

    PHP Code:
    <?php

    eval("\$body = \"$body\";");
    echo 
    $body"\n";


    ?>
    The results are:

    Dear Joe,
    This is an email . . . .
    From Me

    instead of

    Dear $name
    This is an email . . .
    From Me

    I hope that helps some! I think I've understood your problem and hope that helps to solve it . . . check out the PHP manual for more info on eval()


  25. #25
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks gfawce1 that works an absolute treat and its exactly what i was looking for!!

    Thanks again!!

    james


Bookmarks

Posting Permissions

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