Smicks Posted June 9, 2008 Share Posted June 9, 2008 Ok, I'm having a little trouble and have found nothing to help me with it so far... I'm not sure the easiest was to explain it, but here it goes: I have html stored in my database, which contains php arrays throughout the html, such as {$variable[0]}. When I output the text, it everything else is fine, except it doesn't replace the text "{$variable[0]}" with the value of $variable[0]. It just outputs the variable name between the brackets. When I tried to do it WITHOUT calling it from the database by just making $variable[0] be the string itself, and output it, it worked fine, like: <?php $variable = array("value1", "value2", "value3"); echo "The first value is {$variable['0]}."; ?> And that would output "The first value is value1.", which is what I wanted. What I basically want to know is what's different from the string of text in the php there, and the string of text in the mysql result that makes it mess up? Link to comment https://forums.phpfreaks.com/topic/109449-solved-mysql-string-with-variables-problem/ Share on other sites More sharing options...
Barand Posted June 9, 2008 Share Posted June 9, 2008 Some code would help Link to comment https://forums.phpfreaks.com/topic/109449-solved-mysql-string-with-variables-problem/#findComment-561460 Share on other sites More sharing options...
DarkWater Posted June 9, 2008 Share Posted June 9, 2008 Yeah, we need to know what's IN your MySQL results, and how you're setting the variables. Link to comment https://forums.phpfreaks.com/topic/109449-solved-mysql-string-with-variables-problem/#findComment-561464 Share on other sites More sharing options...
Smicks Posted June 9, 2008 Author Share Posted June 9, 2008 Ok, well my table is laid out like this: id | html 1 | some html {$variable[0]} and some more html 2 | more html {$variable[0]} and even more html and the query I'm using (i'm already logged in and have the right DB selected) is: <?php $variable = array("value1", "value2", "value3"); $get_html = sprintf("SELECT html FROM html_templates WHERE id='1'"); $get_html_query = mysql_query($get_html); $the_result = mysql_fetch_assoc($get_html_query); echo $the_result; ?> That'll output the EXACT string from the database, with the array: "some html {$variable[0]} and some more html" While it SHOULD say "some html value1 and some more html" Please tell me if I need to be more descriptive. Link to comment https://forums.phpfreaks.com/topic/109449-solved-mysql-string-with-variables-problem/#findComment-561530 Share on other sites More sharing options...
DarkWater Posted June 9, 2008 Share Posted June 9, 2008 So the PHP code is IN your database? Don't do that. =/ Link to comment https://forums.phpfreaks.com/topic/109449-solved-mysql-string-with-variables-problem/#findComment-561535 Share on other sites More sharing options...
Smicks Posted June 9, 2008 Author Share Posted June 9, 2008 No, not any code, really, just the array variable between the two braces. I want the variable and braces themselves to be replaced with the value of the variable, just like it would be if it were in a normal string of text, like I said in my first post. I'm trying to figure out why that won't happen when i output the string from the database. Even if the variable wasn't defined, which it is, it would just leave an empty value instead of outputting {$variable} like it is. I'm not sure if it needs special formatting or something, so that's why I posted Link to comment https://forums.phpfreaks.com/topic/109449-solved-mysql-string-with-variables-problem/#findComment-561555 Share on other sites More sharing options...
DarkWater Posted June 9, 2008 Share Posted June 9, 2008 Because PHP won't evaluate it unless you set the string. And don't use the eval() function. Find a better way to do it. Try str_replace or something. Link to comment https://forums.phpfreaks.com/topic/109449-solved-mysql-string-with-variables-problem/#findComment-561571 Share on other sites More sharing options...
Smicks Posted June 9, 2008 Author Share Posted June 9, 2008 Actually, eval() was exactly what I needed to use. Works now. Link to comment https://forums.phpfreaks.com/topic/109449-solved-mysql-string-with-variables-problem/#findComment-561589 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.