Re: Problem : replacing of variable with it's value in a string(query)
| From: | Matthew Leverton | Date: | Fri, 16 Jun 2000 16:37:00 +0000 |
| Subject: | Re: Problem : replacing of variable with it's value in a string(query) | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-2084@lists.php.net to get a copy of this message | ||
Correct me if I'm wrong, but you essentially have this scenario:
A] "SELECT * FROM table WHERE column='$x'" is a string that
is stored in a database.
B] You use SQL to grab the string into a PHP variable.
C] You then want to take that PHP variable and execute it as a
SQL statement.
And your trouble is when that string that you pull from the
database has a PHP variable in it?
If I follow you, then I believe the function that you will want
to use is the eval() function. It allows a string to be evaluated
as PHP.
Check out the docs at @ /manual/function.eval.html .
<example>
Example 1. Eval() example - simple text merge
1
2 <?php
3 $string = 'cup';
4 $name = 'coffee';
5 $str = 'This is a $string with my $name in it.<br>';
6 echo $str;
7 eval ("\$str = \"$str\";");
8 echo $str;
9 ?>
10
The above example will show:
1
2 This is a $string with my $name in it.
3 This is a cup with my coffee in it.
4
</example>
The general logic would be:
get_sql_string_from_database();
eval_the_string();
execute_sql_string();
If you have problems doing it, you can send me a private
mail and I'll see if I can help you.
--
Matthew Leverton - matthew@aeroinc.net -
http://www.allegro.cc
-----Original Message-----
From: Zak Greant <zak@nucleus.com>
To: Bobby Singh <bs@corepeople.dk>; php-general@lists.php.net
<php-general@lists.php.net>
Date: Friday, June 16, 2000 11:05 AM
Subject: Re: [PHP-GENERAL] Problem : replacing of variable with it's value
in a string(query)
>At 11:45 AM 6/16/00 +0200, Bobby Singh wrote:
>>I have my sql-queries saved in a database. Every time I want to run a
query
>>I fetch the desired query from the db. It runs fine. The problem is when
the
>>query is dynamic. Here is one of my queries.
>>
>>SELECT * FROM DataTypes WHERE type = '$type'
>>
>>I would like the variable to be replaced with its value before the query
is
>>executed. How do I do that it. I have tried the query with both the
single,
>>double and no quotes. No luck!
>
>Hi Bob,
>
>Something like:
>
>$query = "SELECT * FROM DataTypes WHERE type = '$type'";
>$query_handle = mysql_query ($query);
>
>If it doesn't, then I usually drop a print statement in front of the $query
>line. This lets me see exactly what is generated for the query.
>