
- You are not logged in. | Login
February 28, 2007 12:34 pm
- stp2233
- Member


Variable within a base
How to insert a variable into the database?
I.e. there is, for instance, such a table in the base: “bla bla ASD bla bla”
I need to have $asd variable instead of ASD in the PHP-script when putting it out in the flow.
If I simply add “bla bla $asd bla bla” into the base, $asd is put out as text not as a variable.
February 28, 2007 12:38 pm
- yendii
- Member


Re: Variable within a base
The question isn’t clear enough…
But if we assume that its author asked properly, the answer will be ‘nowise’ 
February 28, 2007 12:43 pm
- stp2233
- Member


Re: Variable within a base
There is $asd variable in the script and there is table “bla bla ASD bla bla” in the database.
I put out this table in this PHP-script. And I’d like to have $asd variable instead of ASD in this part of a table.
How to do it?
February 28, 2007 12:46 pm
- yendii
- Member


Re: Variable within a base
I doubt that there is table like “bla bla ASD bla bla” in the data base.
Does it mean you want to make string “bla bla ASD bla bla” into “bla bla $asd bla bla” by the condition that $asd exists in the code?
I presume that $asd value is already defined by the moment of string handling?
February 28, 2007 12:53 pm
- Stre@m
- Member


Re: Variable within a base
Maybe it’s something like this?
$answer = 42;
$from_db = 'bla bla bla answer bla bla bla';
$vars = get_defined_vars();
foreach ($vars as $name=>$value) {
if (is_scalar($value)) {
$from_db = str_replace($name, $value, $from_db);
}
}
// $from_db == 'bla bla bla 42 bla bla bla';
February 28, 2007 12:59 pm
- SRG
- Member


Re: Variable within a base
Stre@m, from all your code the author of the thread will need only str_replace.
P.S. As for me, get_defined_vars() and is_scalar($value) are especially nice.
February 28, 2007 1:03 pm
- tucansam
- Member


Re: Variable within a base
yendii, put in the base “bla bla ASD bla bla” and use sprintf();
February 28, 2007 1:07 pm
- yendii
- Member


Re: Variable within a base
I have nothing to with it at all
The guy needs to interpolate ASD with value of variable with the same name in the string “bla bla ASD bla bla”. It can be managed different ways, for instance, preg_replace () or str_replace (). The second variant is preferable.


