MySql random display

Hey guys,
This is my first time posting here so if I miss out any common etiquette please let me know and I will correct myself. I’m currently coding a personal site just for a bit of experience but am still very new to PHP. I’ve pasted my code below I am using to randomly display member profile images and the URL to their profile page;

$num_displayed = 1 ;
$result = mysql_query ("SELECT * FROM users, user_photos ORDER BY rand() LIMIT $num_displayed;");
while ($row = mysql_fetch_array($result)) 
echo "<a href=\\"".$row["username"]."\\">
<img style='border:1px solid #021a40; padding:1px; width:50px; height:50px;' src=\\"/users/".$row["username"]."/pics/thumbs/".$row["reference"]."\\"border=0 alt=\\"".$row["username"]."\\">

I am displaying the random profiles using this code on the pages to be displayed;

<div class="randimg">
	<?php include "display.php"; ?>
	<?php include "display.php"; ?>
	<?php include "display.php"; ?>
	<?php include "display.php"; ?>
	<?php include "display.php"; ?>

What I aim to achieve is to prevent any duplicate profiles from being displayed, a profile can only be displayed once on a single page load. I’m getting a load of duplicates upon page load and have heard you can use array splice to overcome this. Very sorry but I’m a little bit lost and am in desperate need of some guidance on how to implement this.

Any help is greatly appreciated, thank you very much in advance!

Welcome to the SP forums.

  1. Why do you include the same file 5 times?
  2. Where exactly are you getting a load of duplicates?

this is a cross join

every row in user_photos, regardless of who the photos belong to, is matched up with every single user

no wonder there are dupes


Nice spot Rudy.

ajb225, can you post some more information on how these two tables are related?

hey guys,
Thanks a lot for the quick input. Your right, I think I need to sort out the issue of the incorrect image being pulled firstly!

The script is meant to pull random usernames and pictures and print them to the screen in 50 by 50 blocks, it queries two tables users and user_photos.
‘references’ contains the path to the image stored in a file structure like users/andrew burchell/pics/thumgs/image.jpg

When it queries randomly it also randomizes the reference and tries to display other images relating to other user IDs. I know Im missing something soo obvious and stupid but just cant get my head around how to query the relevant reference in relation to the ID/username being used from users. Ive attached my entire script in a zip because i know my ways of explaining would confuse the most patient reader!

Are the photos stored in the database? If it’s just the links to them stored in the database then you could get the usernames and photo links from the database and then use php’s array_rand() function to pick 50 random username and photo link combinations.

your query needs an INNER JOIN, not a cross join, where each user is matched to his or her own image…

SELECT users.username
     , user_photos.photo_path
  FROM users
  JOIN user_photos 
    [COLOR="Blue"]ON user_photos.user_id =[/COLOR]
    BY rand() LIMIT $num_displayed


Thank you soo much guys for all the advice.

r937 - you have absolutely saved my day I cant thank you enough. Im not in a situation that I can pay for thanks but Ive got 2 domains I bought for a project and am not using anymore. So as repayment I would like to give you them as a way to say thank you. The domains are and - they are with GoDaddy so if you have an account open PM me and I will email them to transfer the domains into your account and change the registrant details to you

Thanks again guys

that’s an awfully nice gesture, and i do appreciate the sentiment, but i’m afraid i cannot use “server smack” domains, i have no idea what i could use them for

i am ~so~ not a server guy, if you know what i mean