PHP/MYSQL - Possible to use php variable inside of mysql field value?


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


(php code)

$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?

Here are the two queries i am working with:

$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 model results.');
} else {
	while($row = mysql_fetch_assoc($sql15)) {
	 $model = ($row['model']);

$linkQuery2 = 'SELECT , 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 link results.');
} else {
	$pageContent .= '';
	while($row = mysql_fetch_assoc($sql3)) {
	 $linkAd = stripslashes($row['link']);
	 $linkDesc = ($row['desc']);
	 $linkDomain = ($row['domain']);
	 $pageContent .= '
	    <li><a href="'.$linkAd.'" target="_tab">'.$linkDesc.' '.$linkDomain.'</a></li>

Basically, I want to use


as part of a value inside of the desc field. When


is echoed, it should display the text along with the $model value, not just spit out


What am i missing?

You need to use " for it to display the contents of the variable:

echo "This is $variable wow";

What do you mean exactly?

Have a look at this page: free4blackberry - Free BlackBerry® Torch™ 9800 Application and Software Downloads

at the bottom you will see a list of links, the 7th link from the bottom, it links to en.softonic, look at the link and you can see my problem :confused:

It should display the value of $model but it just displays as ‘.$model.’

Hmm, it seems like the only solution I can find is to use the php eval() function.

I have read a lots of pages and posts on why using eval is terrible, but it seems like that is only the case when using it with data that is input by the user. As I am not, I see no real reason to worry.

I need some help with the use of this function, I am having trouble trying to get it to actually work in my case. All I get is:

Parse error: syntax error, unexpected T_STRING in /path/link.php(30) : eval()'d code on line 1

Could anyone help me out here with an example please?

I have tried :

$linkDesc = eval($row['desc']);


$pageContent .= '
	    <li><a href="'.$linkAd.'" target="_tab">'.eval("$linkDesc").' '.$linkDomain.'</a></li>

Changing this:

<li><a href="'.$linkAd.'" target="_tab">'.eval("'.$linkDesc.'").' '.$linkDomain.'</a></li>

And this:

$linkDes = ($row['desc']);
$linkDesc = eval("linkDes");

And a lot of other combinations…

Which leads me to think that this is not the answer, perhaps what I need is to serialize $linkDesc?

Figured it out, needed to use preg_replace