ched999uk Posted June 12, 2009 Share Posted June 12, 2009 Hi everyone, I have a problem that is making me pull my hair out. Here goes: I have the following text in a mysql field '$City is in the County of $County' and within my php page I have $City = "Liverpool" $County = "Merseyside" What I want to happen is when the mysql field is echoed within the page it shows 'Liverpool is in the County of Merseyside' but it actually shows '$City is in the County of $County' Any idea how I get the mysql variables to use the page variable values? I hope that makes sense as it is driving me nuts. Please help....... Link to comment https://forums.phpfreaks.com/topic/161995-solved-variables-from-mysql-to-echo-page-variable-value-on-page/ Share on other sites More sharing options...
tom2oo8 Posted June 12, 2009 Share Posted June 12, 2009 Have you tried: echo $City . "is in the county of" . $County; Link to comment https://forums.phpfreaks.com/topic/161995-solved-variables-from-mysql-to-echo-page-variable-value-on-page/#findComment-854721 Share on other sites More sharing options...
Ken2k7 Posted June 12, 2009 Share Posted June 12, 2009 Variable interpolation doesn't work with single quotes. Use double quotes. "$City is in the Country of $Country"; Link to comment https://forums.phpfreaks.com/topic/161995-solved-variables-from-mysql-to-echo-page-variable-value-on-page/#findComment-854725 Share on other sites More sharing options...
ched999uk Posted June 12, 2009 Author Share Posted June 12, 2009 Cheers Tom2008. Thanks but I cant do that because '$City is in the County of $County' is the actual text in the mysql field and I need it to read the variable from the page and display the correct local page variables in place of the $City and $County. This mysql field is used on lots of pages and is required to use the local variable values. The actual mysql database tables are a more complex I am just using the above to try and simplify. Thanks again for an amazingly quick reply. Link to comment https://forums.phpfreaks.com/topic/161995-solved-variables-from-mysql-to-echo-page-variable-value-on-page/#findComment-854727 Share on other sites More sharing options...
ched999uk Posted June 12, 2009 Author Share Posted June 12, 2009 Variable interpolation doesn't work with single quotes. Use double quotes. "$City is in the Country of $Country"; Cheers, unfortunately that doesn't seem to work either. Thanks Link to comment https://forums.phpfreaks.com/topic/161995-solved-variables-from-mysql-to-echo-page-variable-value-on-page/#findComment-854745 Share on other sites More sharing options...
tom2oo8 Posted June 12, 2009 Share Posted June 12, 2009 Cheers Tom2008. Thanks but I cant do that because '$City is in the County of $County' is the actual text in the mysql field and I need it to read the variable from the page and display the correct local page variables in place of the $City and $County. This mysql field is used on lots of pages and is required to use the local variable values. The actual mysql database tables are a more complex I am just using the above to try and simplify. Thanks again for an amazingly quick reply. so its litrally like $City is in the County of $County' including the quotes? if it is try this : $try= '$City is in the County of $County'; $try = str_replace ("$City", "'. $City . '", $try); $try = str_replace ("$County", "'. $County . '", $try); echo $try; Long way of doing things but might get the job done i dont know if it works you could make a function out of it: funtion replacethings($string,$astr, $bstr) { $replaced ="'. " . $astr . " . '"; $replaced2 ="'. " . $bstr . " . '"; $string= str_replace ($astr, $replaced, $string); $string = str_replace ($bstr, $replaced2, $string); return $string } for example: $string = replacethings($oldstring,"$City","$County"); Link to comment https://forums.phpfreaks.com/topic/161995-solved-variables-from-mysql-to-echo-page-variable-value-on-page/#findComment-854756 Share on other sites More sharing options...
ched999uk Posted June 12, 2009 Author Share Posted June 12, 2009 Cheers Tom2008. Thanks but I cant do that because '$City is in the County of $County' is the actual text in the mysql field and I need it to read the variable from the page and display the correct local page variables in place of the $City and $County. This mysql field is used on lots of pages and is required to use the local variable values. The actual mysql database tables are a more complex I am just using the above to try and simplify. Thanks again for an amazingly quick reply. so its litrally like '$City is in the County of $County' including the quotes? It is literally just $City is in the County of $County without the quotes in the mysql field. Link to comment https://forums.phpfreaks.com/topic/161995-solved-variables-from-mysql-to-echo-page-variable-value-on-page/#findComment-854758 Share on other sites More sharing options...
tom2oo8 Posted June 12, 2009 Share Posted June 12, 2009 The thing i posted above wont work then, And i cant edit it! ive got it to work on my server, but your gunna have to adapt it to your needs: <? $City = "Liverpoole"; $County = "theotherthing"; //as if selecting from mysql database $string = "$City is in the County of $County"; echo $string; ?> Link to comment https://forums.phpfreaks.com/topic/161995-solved-variables-from-mysql-to-echo-page-variable-value-on-page/#findComment-854766 Share on other sites More sharing options...
ched999uk Posted June 12, 2009 Author Share Posted June 12, 2009 The thing i posted above wont work then, And i cant edit it! ive got it to work on my server, but your gunna have to adapt it to your needs: <? $City = "Liverpoole"; $County = "theotherthing"; //as if selecting from mysql database $string = "$City is in the County of $County"; echo $string; ?> Thanks very much I will have a play hopefully one of your answers will work. Thanks very much. I will report back. Link to comment https://forums.phpfreaks.com/topic/161995-solved-variables-from-mysql-to-echo-page-variable-value-on-page/#findComment-854788 Share on other sites More sharing options...
ched999uk Posted June 12, 2009 Author Share Posted June 12, 2009 Cheers Tom2008. Thanks but I cant do that because '$City is in the County of $County' is the actual text in the mysql field and I need it to read the variable from the page and display the correct local page variables in place of the $City and $County. This mysql field is used on lots of pages and is required to use the local variable values. The actual mysql database tables are a more complex I am just using the above to try and simplify. Thanks again for an amazingly quick reply. so its litrally like $City is in the County of $County' including the quotes? if it is try this : $try= '$City is in the County of $County'; $try = str_replace ("$City", "'. $City . '", $try); $try = str_replace ("$County", "'. $County . '", $try); echo $try; Long way of doing things but might get the job done i dont know if it works you could make a function out of it: funtion replacethings($string,$astr, $bstr) { $replaced ="'. " . $astr . " . '"; $replaced2 ="'. " . $bstr . " . '"; $string= str_replace ($astr, $replaced, $string); $string = str_replace ($bstr, $replaced2, $string); return $string } for example: $string = replacethings($oldstring,"$City","$County"); I managed to sort it in the end by using : $your_mysql_string = str_replace(array("\$City","\$County"),array($City,$County),$your_mysql_string); So it needed double quotes and a backslash!!!!! Thanks very much for your help. I am now going to watch TV and have a Cool Beer. Thanks. Link to comment https://forums.phpfreaks.com/topic/161995-solved-variables-from-mysql-to-echo-page-variable-value-on-page/#findComment-854801 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.