Forum Moderators: coopster

Message Too Old, No Replies

Same DB Table, 2 Queries, 2 WHERE's

Adding a 2nd Query to Product Page

         

actolearn

9:46 pm on Apr 5, 2016 (gmt 0)

10+ Year Member



Hello ~ I am adding a "Related Products" section at bottom of my product page. Below works fine but wondering if there is a better way to do this.

I am using MySQL (not MySQLi) with "includes"

2nd query uses duplicate fields but fewer than the 1st query plus added an add'l field (f13), and WHERE is different.

<?php
//select items from db
$items = mysql_query ("SELECT f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12 FROM tableOne WHERE myproductNo='x000'") or die(mysql_error());

while($item = mysql_fetch_array($items))
{
?>

MAIN PRODUCT STUFF


<?php
}
?>

RELATED PRODUCTS below


<?php
//select items from db
$items = mysql_query ("SELECT f2, f3, f4 FROM tableOne WHERE status='live' AND f13-which-is-not-included-in-query-above='category1' ORDER BY dbNo ASC LIMIT 0,4") or die(mysql_error());

//extract every item
while($item = mysql_fetch_array($items))
{
?>

4 related products


<?php
}
?>

Thank you ~

actolearn

10:55 pm on Apr 6, 2016 (gmt 0)

10+ Year Member



Anyone?

coopster

2:38 pm on Apr 21, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Are you asking if there is a way to get it done in one query? If so, from what I see in your logic so far I do not believe so. If it is working, and performance is fine, then go with it. You may want to consider one thing though, switch over to mysqli functions.

actolearn

6:13 pm on Apr 21, 2016 (gmt 0)

10+ Year Member



Finally - thank you so much for responding. It does work with two separate queries but I did notice a slight lowering of speed.

I'll look into mysqli. Is it really so much better? I've read many forums where people advise to switch but I don't really understand why.

actolearn

6:38 pm on Apr 21, 2016 (gmt 0)

10+ Year Member



Never mind - I've just read enough to see it needs to be done.