Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
how to parse PHP vars stored in Db?
Old 12-28-2007, 12:44 PM how to parse PHP vars stored in Db?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
hey

okay i have a games script which i am re-vamping and i want to be able to control all the messages shown to the user through the databse so i been doing through all the scripts and on my way been replacing the comments and stuff with my function

get_msg(ID)

and i then have my database with the messgae:

You beat {$r['username']}!!<br />
for example and my function gets it from the db

My Function:
PHP Code:
function get_msg($id)
{
global 
$db$r;
$txt_res $db->query("SELECT * FROM msg_texts WHERE text_id='$id'");
$txt_info $db->fetch_row($txt_res);
return 
$txt_info['text'];

but i just noticed that the PHP in the messages aint being parsed.

do what am i doing wrong?

i thought adding $r to the globals would fix but obviouslt not...

Thanks TP for working answer.
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 12-28-2007, 01:04 PM Re: how to parse PHP vars stored in Db?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
When you store the code in a DB, you need to eval() it if you want the PHP engine to parse it, otherwise, it's just a text string.

PHP Code:
function get_msg($id)
{
global 
$db$r;
$txt_res $db->query("SELECT * FROM msg_texts WHERE text_id='$id'");
$txt_info $db->fetch_row($txt_res);
$text=$txt_info['text'];
eval(
"\$outText=$text");
return 
$outText;

This should do it.
PHP will interpret what is passed into the eval function like a single file, so don't forget to escape every real variable you want to get back.
</span></span>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 12-28-2007, 03:00 PM Re: how to parse PHP vars stored in Db?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
ok so just to clarify, i just add that eval function and as long as i have all my variables to be parsed saved as {$val} and any ' or " are escaped im okay?

Da
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 12-28-2007, 03:05 PM Re: how to parse PHP vars stored in Db?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
okay tried this:

PHP Code:
function get_msg($id)
{
global 
$db$r;
$txt_res $db->query("SELECT * FROM msg_texts WHERE text_id='$id'");
$txt_info $db->fetch_row($txt_res);
 
$txt_info['text'] = eval($txt_info['text']);  
  
return 
$txt_info['text'];

and got this error:

Code:
Parse error: syntax error, unexpected T_STRING in /home/danmike/public_html/global_func.php(509) : eval()'d code on line 1
so im guessing im doing something wrong can you correct?

Thanks,
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 12-28-2007, 03:59 PM Re: how to parse PHP vars stored in Db?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
yep...
Try:
PHP Code:
$txt_info['text'] = eval("\$return={$txt_info['text']}"); 
Remember, you give eval a string, and it interprets it like a file.
You need to tell him where to store the result of the eval.
Here, in a $return variable. And to make that variable seen like a real PHP variable definition, you need to escape the $, to be sure it don't go translated to something before the eval call.
</span></span>
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 12-28-2007, 04:19 PM Re: how to parse PHP vars stored in Db?
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
i got it in the end with a little help from a friend

thanks a lot

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to how to parse PHP vars stored in Db?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.18734 seconds with 12 queries