开发者

PHP MYSQL - Echo mysql value containing a PHP variable?

开发者 https://www.devze.com 2023-03-20 06:18 出处:网络
I am trying to get a variable to display, when used as part of a value inside of a mysql table. $variable = \'cool\';开发者_StackOverflow社区

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

$variable = 'cool';开发者_StackOverflow社区

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>
    ';
    }
}


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


use double quotes :

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


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.


Try this:

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消