3 Replies - 620 Views - Last Post: 23 April 2012 - 04:17 AM Rate Topic: -----

#1 OpenBSD  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 15-August 10

php select random query from two mysql table

Posted 23 April 2012 - 01:53 AM

I select 10 random query form mysql products table when products is featured AND approved . so, now i need to show 10 random image from gallery table when productid of image gallery table Equal of id of products table ( 10 random result / approved / featured ) . my coded not worked ! and i see one blank images in images list gallery . how to fix this ? i think JOIN method is better. how to work my idea with JOIN method ? THANKS

i have this :

$sql = 'SELECT id FROM products WHERE approved = 1 AND badge = "B" ORDER BY RAND() LIMIT 10 ';
$result = $db->query ($sql);
$row = $db->fetcharray($result);
while ($row = $db->fetcharray($result))
{
$sql = 'SELECT id,productid FROM gallery WHERE productid = '.$row['id'].' ORDER BY RAND() LIMIT 1';

 // fetch data and print in <img> and <li> .......

}


Is This A Good Question/Topic? 0
  • +

Replies To: php select random query from two mysql table

#2 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 590
  • Joined: 12-October 09

Re: php select random query from two mysql table

Posted 23 April 2012 - 02:09 AM

SELECT p.id, g.productid, g.id as galleryid
FROM products p 
INNER JOIN gallery g ON g.productid = p.id
WHERE p.approved = 1 AND p.badge = "B" 
ORDER BY RAND() 
LIMIT 10 



or summit like that.
Was This Post Helpful? 1
  • +
  • -

#3 OpenBSD  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 15-August 10

Re: php select random query from two mysql table

Posted 23 April 2012 - 02:36 AM

not worked for me !
Was This Post Helpful? 0
  • +
  • -

#4 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 590
  • Joined: 12-October 09

Re: php select random query from two mysql table

Posted 23 April 2012 - 04:17 AM

It's not exact, it's just roughly how you would do it. Have a play with it and see what you can do.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1