0

i have a situation where i need to be able to store variables from mysql. the pages i am creating are dynamic and contain different user data, what im chasing is the following

//stored in user database
$firstname = "BOB"
$lastname = "MARLEY"

//how i want th content from a mysql database to display in html
hello my name is BOB MARLEY

//how it displays at the moment
hello my name is $firstname $lastname

i cant seem to work it out at all

this is my current database code

<?php
        $firstname= "BOB";
        $lastnamename= "marley";
    $get_content= mysqli_query($con,"SELECT * FROM tbl_forms_content WHERE forms_content_formid='1' LIMIT 1 ");
    while($found_content = mysqli_fetch_array($get_content))
    { 
        echo $found_content['forms_content_content'];
    }   
?>  

any help is greatly apreciated

12
  • You should use a template system. Commented Jan 11, 2015 at 23:03
  • for a start it should be $found_content['firstname'] assuming thats already in the db? Commented Jan 11, 2015 at 23:03
  • Where do you connect to the database? Commented Jan 11, 2015 at 23:03
  • Why do you set firstname and lastnamename in the while loop rather than using the db results? Commented Jan 11, 2015 at 23:04
  • 1
    but why is the name hard coded in the loop then? Commented Jan 11, 2015 at 23:06

2 Answers 2

2

The simplest way would be to str_replace them out;

echo str_replace(array('$firstname', '$lastname'), array($firstname, $lastname), $found_content['forms_content_content']);

Can see a working example here; http://codepad.org/xFEowbMZ

Sign up to request clarification or add additional context in comments.

1 Comment

Way better solution than using eval() +1
0

The right solution is to use a template library. To do what you're trying, you need to use eval:

$firstname= "BOB";
$lastname= "marley";
while($found_content = mysqli_fetch_array($get_content))
{ 
    eval ('echo "' . $found_content['forms_content_content'] . '";');
}

This is very dangerous. If the database contains values that can be set by the user, they could execute any PHP code from it.

6 Comments

I don't think eval can be used without (). Though it would look a lot better.
It needed a semicolon in the argument to eval(), try it now.
yep thats working now, :-) obviously this isnt really recommend but this is for some dynamic pdf forms, is there any other solutions to get the same result
firstname db field, lastname db field
when running this in php works fine when running inside fpdf it just executes and doesnt display the pdf document just html
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.