• You are not logged in. | Login

Post a reply

February 28, 2007 12:34 pm

stp2233
Member
Ranks

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
Ranks

Re: Variable within a base

The question isn??™t clear enough??¦ weird  But if we assume that its author asked properly, the answer will be ???nowise??™  tongue


 

 

February 28, 2007 12:43 pm

stp2233
Member
Ranks

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
Ranks

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:47 pm

stp2233
Member
Ranks

Re: Variable within a base

You are right.


 

 

February 28, 2007 12:53 pm

Stre@m
Member
Ranks

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
Ranks

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
Ranks

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
Ranks

Re: Variable within a base

I have nothing to with it at all  smile  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.


 

 
  • Actions
  • Top