$query = "SELECT src.id FROM (SELECT id FROM src ORDER BY RAND() LIMIT 1) AS randsrc JOIN src ON src.id = randsrc.id WHERE randsrc.id NOT IN (".$_SESSION['id'].")"; I do not have a way to test this, so I hope it works
I should note that there may be other methods, which are optimized for such a select. I used a sub-query to fetch the random ID and then self-joined to the primary table. Just an option.
OK I tried this: if(isset($_SESSION['idtrack'])){ $query = "SELECT src.id FROM (SELECT id FROM src ORDER BY RAND() LIMIT 1) AS randsrc JOIN src ON src.id = randsrc.id WHERE randsrc.id NOT IN (".$_SESSION['id'].")"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $id = $row['id']; $_SESSION['idtrack'] = $_SESSION['idtrack'].",".$id; }else{ $query = "SELECT id FROM src ORDER BY RAND() LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $id = $row['id']; $_SESSION['idtrack'] = $id; } echo "ID(s) = ". $_SESSION['idtrack']; PHP: But I get this: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in *path removed* on line 54 ID(s) = 4, Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in *path removed* on line 109 As you can see the code after else works (since the first id displays) but the code before it returns an error. Any idea whats wrong? Thanks in advance
OK, I fixed the previous error. I accidentaly had $_SESSION['id'] instead of $_SESSION['idtrack'] in the first if-statement. Here is the new code: if(isset($_SESSION['idtrack'])){ $query = "SELECT src.id FROM (SELECT id FROM src ORDER BY RAND() LIMIT 1) AS randsrc JOIN src ON src.id = randsrc.id WHERE randsrc.id NOT IN (".$_SESSION['idtrack'].")"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $id = $row['id']; $_SESSION['idtrack'] = $_SESSION['idtrack'].",".$id; }else{ $query = "SELECT id FROM src ORDER BY RAND() LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $id = $row['id']; $_SESSION['idtrack'] = $id; } echo "ID(s) = ". $_SESSION['idtrack']; PHP: However, I'm still getting an error. First it displays 2 ids, which is strange, but there are no errors. Then it continues to display 1 id at a time, like it should. Then it randomly gets to a point where it doesn't display an id. At this point it returns the following: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in *path removed* on line 54 ID(s) = 7,9,11,8,, Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in *path removed* on line 109 Code (markup): Note that it doesn't stop showing ids because it runs out of unique ids! There are still many ids that it could have shown. Any ideas? Thanks for helping out.
I think I know what is wrong. How many rows are in the source table? I ask because it appears that the RAND() function may be returning an id that is already in session, so no results. This would explain why you have 8,, in session. Also, I suggest restructuring the code for maintainability purposes. I was up to 4am last night helping a member and I am still a bit tired, will work on the code in the morning. Lastly, glad I was able to offer options. It helps me to brainstorm with others too.
There are 13 rows in the source table. That makes sense, but I'm not sure how to prevent that from happening. Alright, I'll work on cleaning it up. Thanks for helping. I'll definitely give you plus reputation once the problems are worked out.