2

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>
    ';
    }
}
3
  • 1
    Looks like your quote marks are mismatched. Try setting the db value to "This is ".$variable." wow" Commented Jul 12, 2011 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 Commented Jul 12, 2011 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 Commented Jul 12, 2011 at 18:19

5 Answers 5

3

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.)

Sign up to request clarification or add additional context in comments.

3 Comments

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!
how would str_replace work in this case? $linkAd = ($row['link']); the variable is inside of a value in the field link
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).
2

use double quotes :

echo "This is " . $variable . " wow";

2 Comments

displays as " . $variable . "
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.
2

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.

11 Comments

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?
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.
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.
@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');
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?
|
1

Try this:

"This is '" . $variable . '" wow"

1 Comment

displays as '" . $variable . '"
0

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.

1 Comment

its more complicated than my little example i promise :) the basic idea is there though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.