Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to get a variable to display, when used as part of a value inside of a mysql table.

$variable = 'cool';

MYSQL FIELD VALUE

"This is '.$variable.' wow"

When i echo this value, it displays as:

This is '.$variable.' wow

I want it to display as:

This is cool wow

What is the trick here?

@Mark sorry im new to this

$linkQuery3 = 'SELECT model
FROM models
WHERE model_id = "'.$pageModel.'"
';
$sql15 = mysql_query($linkQuery3) or die(mysql_error());
if (mysql_num_rows($sql15) == 0) {
    die('No results.');
} else {
    while($row = mysql_fetch_assoc($sql15)) {
     $model = stripslashes($row['model']);
    }
}

$linkQuery2 = 'SELECT l.link , l.desc , l.domId , d.domain FROM links l INNER JOIN domains d ON d.domId = l.domId WHERE l.catId="'.$pageCat.'" && (l.modId="1" || l.modId="'.$pageModel.'") ORDER BY domain
';
$sql3 = mysql_query($linkQuery2) or die(mysql_error());
if (mysql_num_rows($sql3) == 0) {
    die('No results.');
} else {
    $pageContent .= '';
    while($row = mysql_fetch_assoc($sql3)) {
     $linkAd = ($row['link']);
     $linkDesc = ($row['desc']);
     $linkDomain = ($row['domain']);
     $pageContent .= '
        <li><a href="'.$linkAd.'" target="_tab">'.$linkDesc.' '.$linkDomain.'</a></li>
    ';
    }
}
share|improve this question
1  
Looks like your quote marks are mismatched. Try setting the db value to "This is ".$variable." wow" –  Matt H. Jul 12 '11 at 17:18
    
tried this and displays as ".$variable." quotes are like that because echo'd field is displayed within another variable, by chance has it got something to do with some kind of declaration? like htmlspecialchars –  IndigoIdentity Jul 12 '11 at 17:24
    
@Mark alright thanks for the edit, that was driving me crazy. What i am trying to do is get the value of $model to show up in the value of the desc field when $linkDesc is echo'd –  IndigoIdentity Jul 12 '11 at 18:19

5 Answers 5

up vote 2 down vote accepted

What you are doing here is trying to echo the string from the database, but replace placeholder text with a specific value. You cannot do this by just storing a string that the PHP parser would treat in a specific way, and expect PHP to treat it the same way when it sees that string at run-time.

Here is what I suggest: Use a more straightforward delimiter for the part of the string you wish to replace, like so:

"This is :variable: wow"

and use str_replace() to echo the right thing. (You can also use sprintf() for the same purpose.)

echo str_replace(':variable:', $variable, $mystring);

(Here $mystring contains the string from the database.)

share|improve this answer
    
sounds like we're both on the same page here, let me let this info soak in and i'll let you know :) thank you! –  IndigoIdentity Jul 12 '11 at 17:31
    
how would str_replace work in this case? $linkAd = ($row['link']); the variable is inside of a value in the field link –  IndigoIdentity Jul 12 '11 at 17:49
    
I'm not sure what you're asking. Are you saying that you want to retrieve the column link and replace a placeholder that appears within? Just use $linkAd = str_replace(':variable:', $variable, $row['link']). Or are you saying that you want to use the value of link to perform the substitution? Use echo str_replace(':variable:', $row['link'], $mystring). –  Hammerite Jul 13 '11 at 17:02
    
Had to use preg_replace –  IndigoIdentity Aug 5 '11 at 21:24

use double quotes :

echo "This is " . $variable . " wow";
share|improve this answer
    
displays as " . $variable . " –  IndigoIdentity Jul 12 '11 at 17:22
1  
OP has the string as literal text in his database. echoing a variable containing the string does not magically make it be treated as PHP code. –  Marc B Jul 12 '11 at 17:29

You'd need to eval() the string you retrieve from MySQL. There's no other way of getting a string to be interpreted as PHP code. eval() should be avoided at all costs, however. You'd be better off using a templating system, or a simple string-substitution system, than eval()ing stuff coming out of a database.

share|improve this answer
    
This is correct. +1. Another question would be, why are you storing the variable in the database? Shouldn't you store the variable contents in the database and reverse the way you're storing/displaying things? –  rockerest Jul 12 '11 at 17:25
    
I don't see a reason to suggest eval() here, when nothing in the OP suggests he wants to do anything other than echo parameterised strings. –  Hammerite Jul 12 '11 at 17:28
    
eval() expects valid JS code. You'd need to do $var = 'cool'; $string = 'This is $var'; eval("\$newstring = '$string'");. But again, eval() is evil. You're not doing simple string substitution, you're trying to write a whole new PHP script on the fly. It's a bad idea. use str_replace() and the like instead. –  Marc B Jul 12 '11 at 17:31
1  
@Hammerite: yes, but OP is doing INSERT INTO table (x) VALUES ('This is $variable'), so that the literal word $variable is in the database. There's no way to get the retrieved string to be interpreted and $varaible substituted with its value without eval, unless you want to do str_replace($string, '$variable', 'cool'); –  Marc B Jul 12 '11 at 17:33
    
See the variable contents are in the database "$model = stripslashes($row['model']);" This is why its a little more complicated than my example. I was thinking stripslashes needs to be replaced, as said, by a directive saying that the field link contains php code being $model? –  IndigoIdentity Jul 12 '11 at 17:35

Try this:

"This is '" . $variable . '" wow"
share|improve this answer
    
displays as '" . $variable . '" –  IndigoIdentity Jul 12 '11 at 17:21

This can be a heck of a lot simpler. Just do this...

echo "This is '$variable' wow";

That is all you need. There is no need to leave the string context and come back in. The outer double-quotes will evaluate the value of a variable. If you had used single quotes on the outside then it would be evaluated literally as $variable and not its value.

So you would write a select statement like this:

$sql = "SELECT * FROM fancytbl WHERE id = '$id' AND myhands = 'diamonds'";

See, its simple.

share|improve this answer
    
its more complicated than my little example i promise :) the basic idea is there though. –  IndigoIdentity Jul 12 '11 at 17:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.