Not 100% sure if I am understanding you correctly, but if I am, this may help you:
<?php
$ord_id = (int)$_GET['ord_id']; // Or whatever you do.
// Query for all the products in the order.
$sql = "SELECT o.`product_name`,
IFNULL(p.product_name, FALSE) AS `has_mobile`
FROM `order_detail_table` AS o
LEFT JOIN `product_table` AS p
ON p.`category_name` = 'Mobile Phones'
AND p.`product_name` = o.`product_name`
WHERE o.`order_id` = $ord_id";
$result = mysql_query($sql) or die(mysql_error());
// Print the table header
echo '<table>';
echo '<tr><th>Product name</th><th>Coupon</th></tr>';
// Print each product
while($row = mysql_fetch_assoc($result))
{
echo '<tr><td>'. $row['product_name'] .'</td>';
// Check if there was a mobile, and print the image
// if there was.
if($row['has_mobile'] !== false) {
echo '<td><img src="images/special_coupon.gif"></td>';
}
echo '</tr>';
}
// Close the table
echo '</table>';
?>
This, based on the two queries in your code, should fetch the name of every product in that order, and check if the product is a mobile phone. Then it simply prints a table, adding an image to the products that are mobile phones.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IFNULL(p.product_name, FALSE) AS has_mobile FROM order_detail_table AS ' at line 2
Odd. The problem is that MySQL is returning 0 as a string for FALSE values. I was sure it returned a boolean FALSE.
Anyhow, to fix it replace this line:
if($row['has_mobile'] !== false) {
with:
if($row['has_mobile'] != false) {
Basically, the “!==” operator strictly checks for a boolean false, while “!=” checks for any value that evaluates as false, like 0 or NULL.
When the product is a mobile, the ‘has_mobile’ field contains the product name, which is a string and thus evaluates as TRUE. If it is not a mobile, it returns a MySQL FALSE, which is returned to PHP as “0”, which PHP evaluates as false.
i tried myself also earlier removing one equal to sign. but got same result.
here is the echo result of sql query
SELECT o.product_name FROM order_detail_table AS o LEFT JOIN product_table AS p ON p.category_name = 'Mobile Phones' AND p.product_name = o.product_name WHERE o.order_id = 334
What I want to see is the result of an invoice, where one of the items is a mobile phone.
SELECT
o.`product_name`,
IFNULL(p.product_name, FALSE) AS `has_mobile`
FROM `order_detail_table` AS o
LEFT JOIN `product_table` AS p
ON p.`category_name` = 'Mobile Phones'
AND p.`product_name` = o.`product_name`
WHERE o.`order_id` = 31173
That is not the correct query. You are missing the ‘has_mobile’ column that the query I posted creates. If you leave it out, as you do there, then all the rows will be missing the image.
SELECT o.`product_name`, IFNULL(p.product_name, FALSE) AS `has_mobile` FROM `order_detail_table` AS o LEFT JOIN `product_table` AS p ON p.`category_name` = 'Mobile Phones' AND p.`product_name` = o.`product_name` WHERE o.`order_id` = 334