Echoing PHP from Database (Widget)

The page display the output when I paste it onto a page. But when I echo it from a database it does do anything.

When I echo code from a database does it have any affect on it?

Do I need to place it inside some form of frame.

When I echo an Amazon frame it displays fine.

<iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=vacuumsacouk-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B004RJ0Y8S" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>

We’re talking about queries not display.

Why does the query work when I paste it on a page but not when I echo it from a database?

Could it be some sort connection problem, although it doesn’t create any errors to suggest that.

What does that mean? “Paste a query on a page”.

Can you please make a test page?

Hi,

When I paste the code onto a .php page the code works as desired. It displays what is in my database.

But when I paste the code into my database and then echo the code onto the page the code doesn’t work.

The query works. The issue I have is when I echo it from a database. It is the process of echoing it from a database onto a page that is causing the problem.

You’re trying to take a string from your DB and have it executed as PHP, aren’t you? It would be possible to do that using [fphp]eval[/fphp], but it’s really not a good idea.

Can I ask why you want to store PHP in the DB?

Are you kidding me? Are saying you are putting this query code in your database? What are you trying to accomplish? There must be a better way.

I am trying to echo a product display onto a blog post.

Im not sure how else I can achieve this. For example if I write a blog post about Super Mario games I want to echo Super Mario games from my database onto that specific blog post.

Make a little function at the top of your page to query table and build result for display. Something like this.

function showproduct($linkname){
	$sql = "
	    SELECT p.name, p.linkname, p.product_id, p.price, p.discount, f.image_link
	        FROM productdbase as p
	        INNER JOIN furniture_groups as f
	            ON
	                f.id = p.id
	        WHERE
	            p.linkname LIKE '%$linkname%'
	    LIMIT 15";
	$result = mysql_query($sql) or die(mysql_error());
	$output = "";
	while ($query_row = mysql_fetch_assoc($result)) {
		$output .= "<div class=\\"productrangeborder\\">
		<div class=\\"productsdetailsborder\\">
		<a href=\\"http://website.co.uk/products/product/{$query_row['product_id']}\\" class='productlink' rel=\\"nofollow\\" >{$query_row['name']}</a>
		</div>
		
		<div class=\\"productimageborder\\">
		<a href=\\"http://website.co.uk/products/product/{$query_row['product_id']}\\" rel=\\"nofollow\\"><img src=\\"{$query_row['image_link']}\\" alt=\\"{$query_row['name']}\\" /></a>
		</div>
		
		<div class=\\"priceborder\\">Price £{$query_row['price']}<br /></div><div class=\\"discountborder\\">Save {$query_row['discount']}%<br />
		</div></div>";
	}
	return $output;
}

Then in your page where you need it, you call the function.

$product = "coffee";
echo showproduct($product); 

Hi,

I tried that but no success.

There is something seriously wrong with echoing the code from the database. I put this simple test on my page which echoes toad.


$test = toad;

echo $test;

?>

But when I try it in echoing it from my database it doesn’t display anything. The issue is echoing the code from the database.

<?php

$test = toad;

echo $test;

?>

Simple answer… don’t do it.

What else can I do?

Keep your data, logic, and presentation separate… the DB should contain just the data (sometimes a little HTML is unavoidable, such as P tags on text etc.), and your logic should live in functions (or objects, if you’re doing OOP), and your HTML should ideally be in separate files with the minimum of PHP necessary to loop and echo your data.

Thanks,

But if a simple peice of PHP code doesn’t work then what can I do?

Are you saying its not possible to echo PHP onto a page and for it to work?

Well it’s design issue. Say you have a blog that talks about “Super Mario games”. Somewhere you’ve defined this as the topic, right? So set this to a variable and make your query based on that. I see no reason you have to have a hard-coded query saved to database for every topic. Call a function if this variable is set.

if (isset($product)){
	echo showproduct($product); 
}

You can even do it without the function if you wish by just wrapping the query in an IF condition.

if (isset($product)){
	
		$sql = "
	    SELECT p.name, p.linkname, p.product_id, p.price, p.discount, f.image_link
	        FROM productdbase as p
	        INNER JOIN furniture_groups as f
	            ON
	                f.id = p.id
	        WHERE
	            p.linkname LIKE '%$product%'
	    LIMIT 15";
	$result = mysql_query($sql) or die(mysql_error());
	$output = "";
	while ($query_row = mysql_fetch_assoc($result)) {
		$output .= "<div class=\\"productrangeborder\\">
		<div class=\\"productsdetailsborder\\">
		<a href=\\"http://website.co.uk/products/product/{$query_row['product_id']}\\" class='productlink' rel=\\"nofollow\\" >{$query_row['name']}</a>
		</div>
		
		<div class=\\"productimageborder\\">
		<a href=\\"http://website.co.uk/products/product/{$query_row['product_id']}\\" rel=\\"nofollow\\"><img src=\\"{$query_row['image_link']}\\" alt=\\"{$query_row['name']}\\" /></a>
		</div>
		
		<div class=\\"priceborder\\">Price £{$query_row['price']}<br /></div><div class=\\"discountborder\\">Save {$query_row['discount']}%<br />
		</div></div>";
	}
	echo $output; 
}

But if you need this display on several pages. Just make a page to hold the function like I posted above and include it at the top of your pages.

EXACTLY

I’m sorry guys but I think you are really missing the point.

What ever code I use I cannot echo it from my database and get it to work.

Even this basic test doesn’t work. Until I resolve this issue it doesn’t matter what code I use.

<?php

$test = toad;

echo $test;

?>

Well, as I said before, technically it’s possible… but as Drummin said, it’s pointing to a problem with your design. Could you post an example of the PHP code you have stored in the DB for one of your products? It’d be easier to give you advice based on what you’re trying to output.

READ http://www.sitepoint.com/forums/showthread.php?1165967-Echoing-PHP-from-Database-(Widget)&p=5560761&viewfull=1#post5560761