We help IT Professionals succeed at work.

replace a variable from result of query mysql

Hello EE
into my php page i have a variable $owner
$owner = "pinco";

from a query of my db mysql i have a result like this:
"bla bla bla $owner bla bla bla"

i would print $owner of my result db whit value of variable of my page php.
where i wrong?
regards
Denis
Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2016

Commented:
http://www.laprbass.com/RAY_temp_denny3d.php

Outputs: bla bla bla pinco bla bla bla
<?php // RAY_temp_denny3d.php
error_reporting(E_ALL);

// SET A VARIABLE
$owner = 'pinco';

// USE THE VARIABLE IN A STRING
$str = "bla bla bla $owner bla bla bla";

// SHOW THE WORK PRODUCT
echo $str;

Open in new window

Author

Commented:
Hi Ray
dont work, i post my code:
into db i have this string: <p>Questo sito è di proprietà e viene gestito da $owner</p>
into my page i have this variable: $owner = 'pinco';

this is my code:
$sel_privacy = "SELECT id, titolo, body, FROM table";
$ris_privacy = mysql_query($sel_privacy);
$privacy = mysql_fetch_object($ris_privacy);
echo'<h1>' . $privacy->titolo . '</h1>';
echo'<p>';
echo $privacy->body;
echo'</p>';

I have this result:
Questo sito è di proprietà e viene gestito da $owner

where i wrong?
regards
Most Valuable Expert 2011
Top Expert 2016
Commented:
I am guessing that this is in "body"
Questo sito è di proprietà e viene gestito da $owner

So you might use something like this

$pbody = (string) $privacy->body;
$pbody = str_replace('$owner', $owner, $pbody);
echo'<p>';
echo $pbody;
echo'</p>';

Note the use of single quotes.

HTH, ~Ray

Author

Commented:
excellent