Echoing PHP from Database (Widget)

Hi,

I am trying add a widget to certain blog post so that it display products. Just like an image would appear. The code works fine when I paste onto a standard page but when I try to echo it from a database it displays the second peice of PHP code as it was standard text.

Any suggestions please?

<?php echo $query_row['name']; ?>
<?php

$query = mysql_query("
    SELECT name, linkname, product_id, price, discount, image_link
        FROM productdbase p
        INNER JOIN furniture_groups f
            ON
                f.id = p.id
        WHERE
            linkname LIKE '%coffee%'
    LIMIT 15");

while ($query_row = mysql_fetch_assoc($query)) { 

?>

<div class="productrangeborder">
<div class="productsdetailsborder">
<a href="http://website.co.uk/products/product/<?php echo $query_row['product_id']; ?>" class='productlink' rel="nofollow" ><?php echo $query_row['name']; ?>
</a>
</div>

<div class="productimageborder">
<a href="http://website.co.uk/products/product/<?php echo $query_row['product_id']; ?>" rel="nofollow"><img src="<?php echo $query_row['image_link']; ?>" alt="<?php echo $query_row['name']; ?>" /></a>
</div>

<div class="priceborder">Price £<?php echo $query_row['price']; ?><br /></div><div class="discountborder">Save <?php echo $query_row['discount']; ?>%<br />
</div></div>

<?php 
 }
?>

You need to add table labels p and f to your field names in the query, e.g. p.name, p.linkname etc. You’ll need that in your WHERE statement as well.
I would quote and bracket your results keys when echoing.

<?php echo "{$query_row['product_id']}"; ?>

I’m not sure what you mean by “You need to add table labels p and f to your field names in the query, e.g. p.name, p.linkname etc” I thought I was already doing that.

Be aware that the mysql_* extension is deprecated as of the current version of PHP and will very likely be removed from the next version of PHP. You should be migrating over to either the mysqli_* extension or to PDO

What I was suggesting was to add table identifier with the fields you are calling. Adding an error check wouldn’t hurt either to show why a query might fail. Again though you should consider switching. As I don’t know what tables belong to each field I will offer this.

$sql = "
    SELECT p.name, p.linkname, p.product_id, p.price, p.discount, f.image_link
        FROM productdbase p
        INNER JOIN furniture_groups f
            ON
                f.id = p.id
        WHERE
            p.linkname LIKE '%coffee%'
    LIMIT 15";
$result = mysql_query($sql) or die(mysql_error());
while ($query_row = mysql_fetch_assoc($result)) { 

Hi,

The code works on a standard page but I cant echo it from a database.

Hi,

Im really confused as to how this code is to work. What is the different between my code and this code.

The code works on a standard page but I cant echo it from a database.

What does that mean?

Are you getting query results?
Do you have database connection in place?
IF your query works then you should see result array using this.

<?php

$query = mysql_query("
    SELECT name, linkname, product_id, price, discount, image_link
        FROM productdbase p
        INNER JOIN furniture_groups f
            ON
                f.id = p.id
        WHERE
            linkname LIKE '%coffee%'
    LIMIT 15");

while ($query_row = mysql_fetch_assoc($query)) {
	echo "<pre>";
	print_r($query_row);
	echo "</pre>";
}
?>

IF you are not getting results, does using the query I posted make a difference?
Does it show an error?

Hi,

The echoes this: "; print_r($query_row); echo “”; } ?>

The code I have been using so far works when I paste onto a page. However I cant get it to work when I echo the code from a database.

I am trying to echo product displays on my blog instead of just a standard .php page but when I echo the code from a database it does not work properly.

Is php available on your site?
Does the page have php extension, e.g. index.php?

Hi it is a .php page.

Is there a method of echoing PHP from a database?

And if you query single table do you get result?

<?php
$sql = "SELECT name, linkname, product_id
        FROM productdbase 
        WHERE
            linkname LIKE '%coffee%'
    LIMIT 15";
$result = mysql_query($sql) or die(mysql_error());
while ($query_row = mysql_fetch_assoc($result)) {
	echo "<pre>";
	print_r($query_row);
	echo "</pre>";
}
?>

It should print out an array.

Knowing what fields belong to which table might help.

Hi,

It echoes: "; print_r($query_row); echo “”; } ?>

It does echo the array when I paste it onto a standard page.

OK, let’s try two tables and identify tables with AS and use this same identifiers with fields. Please edit accordingly.

<?php
$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 '%coffee%'
    LIMIT 15";
$result = mysql_query($sql) or die(mysql_error());
while ($query_row = mysql_fetch_assoc($result)) {
	echo "<pre>";
	print_r($query_row);
	echo "</pre>";
}
?>

Hi,

This echoes: "; print_r($query_row); echo “”; } ?>

Are the identifiers correct in that image_link is from table furniture_groups?
I assume productdbase is your primary table. Maybe try
LEFT JOIN instead of INNER JOIN

Yes they are.

The problem appears to be echoing the code from a database.

Is there some very simple I can try?

You are not getting results from the joined query for some reason. It’s not about echo, it’s about the query. Are you using the error line like in my example?

$result = mysql_query($sql) or die(mysql_error()); 

Please show current test query.

But what is really confusing me is that the code I have works when I paste it onto a standard page. It doesn’t work when I echo it from my database.

Can you show your current test page please?

“The code I have works” doesn’t mean a thing. Are you talking about output display? It’s a bad practice to be making these queries during output anyway but I’m just trying to help you get things working like you intend.

Can you make a little test page that has DB connection at the top and our query?

 <?php
include("conn.php");
$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 '%coffee%'
    LIMIT 15";
$result = mysql_query($sql) or die(mysql_error());
while ($query_row = mysql_fetch_assoc($result)) {
    echo "<pre>";
    print_r($query_row);
    echo "</pre>";
}
?>