Dynamic variables with dynamic values
Moderator: General Moderators
Dynamic variables with dynamic values
Hi,
New on this forum and trying to make a php app.
I am trying to build functions using variables comming from a database and having values also comming from a database.
For defines I have it up and running but for variables I have not ...
I am retreiving 2 fields from database to be variable_name and variable_value
I would like to have something like
$var->$variable_name = $variable_value
this runs fine but I have a variable_value in the database having: date('Y-m-d H:i:s') and this is returned as a string however I would like to have the executed version to be for example 2005-09-20 06:44:10. I tried using eval but no luck so far. Can this be done? Do I mis some simple thing here? I am working with php for a while but still in beginner phase I gues.
Any help apriciated,
Ruud
New on this forum and trying to make a php app.
I am trying to build functions using variables comming from a database and having values also comming from a database.
For defines I have it up and running but for variables I have not ...
I am retreiving 2 fields from database to be variable_name and variable_value
I would like to have something like
$var->$variable_name = $variable_value
this runs fine but I have a variable_value in the database having: date('Y-m-d H:i:s') and this is returned as a string however I would like to have the executed version to be for example 2005-09-20 06:44:10. I tried using eval but no luck so far. Can this be done? Do I mis some simple thing here? I am working with php for a while but still in beginner phase I gues.
Any help apriciated,
Ruud
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
if you want to get the result from eval() you need to tell it to return..
however, using eval() is highly recommended to not be done. it's too easy to have coding errors, and security risks...
Code: Select all
$something = 'date("Y-m-d");';
$result = eval('return '.$something);- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I would store Y-m-d H:i:s in the database instead of it along with the date function..
so you would be able to do
so you would be able to do
Code: Select all
$date = date($row[$dateformat],time());In my case it is not knowing what is in the database. I just have a database having variable_name and variable_value and togheter they do something in this case for example
$var->$db_var_name = $db_var_value could become
$var->date_added = date('Y-m-d') ; but it could also become
$var->date_name = 'db_field_name' ;
in this db_var_name is a comming from the database and db_var_value also. Hence I do not know what is in the variable_value. It could be a number, string or function call. The provided solution worked for all 3 cases.
My question now is this works, but if EVAL is not recomended what is the code to do the same not having the risk EVAL has!
$var->$db_var_name = $db_var_value could become
$var->date_added = date('Y-m-d') ; but it could also become
$var->date_name = 'db_field_name' ;
in this db_var_name is a comming from the database and db_var_value also. Hence I do not know what is in the variable_value. It could be a number, string or function call. The provided solution worked for all 3 cases.
My question now is this works, but if EVAL is not recomended what is the code to do the same not having the risk EVAL has!
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I am writing sort of a aplication in which users can make up their own functions easy, defining variables easy without knowledge of programming .. so far i am pretty good on the way ... flexiblity on database, tables, columns, language etc ... so the part bugging me this moment was the function part ...
In this case i need the "blind" variables because i do not know what user is building!
In this case i need the "blind" variables because i do not know what user is building!
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact: