I have entered the following query in MySQL:
insert into hospital (name,age) values ('william', 'select * from department where age = $agegrp');
Now I have fetched this value (the sql), and trying to execute it. How can i do this?
I have fetched the select statement into a variable called $var.
$agegrp = "10";
$value = mysql_query ($var) or die ('error');
I only get error so something is wrong with my query that I inserted. How can I solve this?
first question for me: why do you store sql code in your hospital table?
it would make more sense if all your sql code would be embedded in your php program code.
if you later change your table design, you won’t have to change the content of the database table.
but to answer your question, the content of your $var is a simple string. what you want is to evaluate it or to replace the string ‘$agegrp’ with the actual value of the variable $agegrp.
so you could do:
this would be a simple solution.