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.
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.
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)) {
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.
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>";
}
?>
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
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());
“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>";
}
?>