Welcome to WebmasterWorld Guest from 18.204.48.199

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Same DB Table, 2 Queries, 2 WHERE's

Adding a 2nd Query to Product Page

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

Junior Member from US 

5+ Year Member

joined:Dec 1, 2012
posts: 90
votes: 0


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 ~
10:55 pm on Apr 6, 2016 (gmt 0)

Junior Member from US 

5+ Year Member

joined:Dec 1, 2012
posts: 90
votes: 0


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

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


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.
6:13 pm on Apr 21, 2016 (gmt 0)

Junior Member from US 

5+ Year Member

joined:Dec 1, 2012
posts: 90
votes: 0


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.
6:38 pm on Apr 21, 2016 (gmt 0)

Junior Member from US 

5+ Year Member

joined:Dec 1, 2012
posts: 90
votes: 0


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