Solved

wrong image shows when using two LIKE expressions

Posted on 2009-04-06
4
154 Views
Last Modified: 2013-12-12
Hello

When I use two expressions to pick out a list of product codes from a database then display them with an image I get the wrong image showing. I have a noimage.gif which displays correctly if I use the expression on its own.

here's the code: I am picking out XST, XPV, XUT and XC. if I do it individually it works. the code for displaying in at the bottom.
$q= "SELECT products.product_id, products.stock_indicator, products.size_number, products.size_desc, products.description, products.trade_price_single, products.trade_price_multi, images.path FROM products, images  WHERE products.product_id LIKE 'XST%' OR products.product_id LIKE 'XPV%' OR products.product_id like 'XUT%' OR products.product_id like 'XC%' AND products.product_id=images.product_id GROUP BY products.product_id ORDER BY products.product_id ASC LIMIT $start,$display";
 

code for displaying the product image
 

echo '	<div class="imagebox"><img src=../praybourne/'. $row['path'] . '></div>';

Open in new window

0
Comment
Question by:Skycapt
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:adrian_brooks
ID: 24083257
Have you confirmed that your product_id fields match in both the products & images table?

Sounds like you have something mismatched.

~A~
0
 
LVL 1

Author Comment

by:Skycapt
ID: 24084851
Hi Adrian

As far as I can tell they match, the funny thing is if I use the expression on its own such as 'XST%' it pulls the correct image and shows correctly, when I couple two expressions together such as XST OR XPV it seems to pick a random image and display that for each row.

Do you need to see the data file?
0
 
LVL 12

Accepted Solution

by:
adrian_brooks earned 500 total points
ID: 24088350
Try surrounding all your OR conditions in parentheses to separate them from your AND condition.
SQL is a rather fickle language in that respect.

So, instead of your existing SQL SELECT statement, perhaps try this;


Something else worth bringing up here though would be, does the images table have multiple images per record that the OR clause would get confused on? Let's assume that each record in the images table can have one or more of those prefixes. When that OR condition is processed, then it will find multiple TRUE conditions and likely return the last known positive match.

~A~
$q= "SELECT products.product_id, products.stock_indicator, products.size_number, products.size_desc, products.description, products.trade_price_single, products.trade_price_multi, images.path FROM products, images  WHERE (products.product_id LIKE 'XST%' OR products.product_id LIKE 'XPV%' OR products.product_id like 'XUT%' OR products.product_id like 'XC%') AND products.product_id=images.product_id GROUP BY products.product_id ORDER BY products.product_id ASC LIMIT $start,$display";

Open in new window

0
 
LVL 1

Author Closing Comment

by:Skycapt
ID: 31567284
Hi Adrian

That worked! Thanks very much
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now