1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

HELP: Getting a row only once in MySQL

Discussion in 'MySQL' started by MCJim, Jun 24, 2008.

  1. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #21
    $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 :)
     
    Social.Network, Jun 28, 2008 IP
  2. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #22
    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.
     
    Social.Network, Jun 28, 2008 IP
  3. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #23
    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
     
    MCJim, Jun 29, 2008 IP
  4. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #24
    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.
     
    MCJim, Jun 29, 2008 IP
  5. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #25
    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.
     
    Social.Network, Jun 29, 2008 IP
  6. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #26
    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.
     
    MCJim, Jun 29, 2008 IP