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. #1
    Hello, I have table with multiple rows. They are each labeled with a separate id. I am randomly calling rows. How can I prevent a row from being randomly selected twice?

    Thanks in advance
     
    MCJim, Jun 24, 2008 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    take a additional true / false field say check with default value false for all rows

    After you select a row make the check field true and select only those which are false

    Regards

    Alex
     
    kmap, Jun 24, 2008 IP
  3. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    While that could work, the problem is if I change the values, other users will get those values. Maybe I have to use cookies?
     
    MCJim, Jun 24, 2008 IP
  4. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #4
    oh you want the selection seperate for each user?

    In that case its lil complicated

    Regards

    Alex
     
    kmap, Jun 24, 2008 IP
  5. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #5
    For that you may need to introduced a new column (say counter) which will save that how many times this row has been displayed. Steps to do this

    1. Select some random rows ORDER BY counter DESC.
    2. Increment the counter against those select rows ids
     
    mwasif, Jun 24, 2008 IP
  6. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    But if I did that, other users would be affected by the column changing. I want each user to be able to load each row once randomly. Would I have to use cookies to achieve this?
     
    MCJim, Jun 24, 2008 IP
  7. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #7
    You may need to use cookies or sessions depending upon your system.
     
    mwasif, Jun 24, 2008 IP
  8. Dan3

    Dan3 Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I don't know how much resources this would consume (depends on the size of your table), but how about using temporary tables? You can use the previous solution by mwasif to increase a counter per chosen row. That way mysql handles the creation / deletion of the temp table for each user (when the session is over, temp table is removed automatically by mysql and each user gets its own temp table).

    From http://dev.mysql.com/doc/refman/5.0/en/create-table.html:
    Hope it helps!
     
    Dan3, Jun 24, 2008 IP
  9. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #9
    In the PHP application, each connection is treated as a session. Connection will be closed once the page execution is completed hence it will loose the temporary table.

    Temporary table is not a solution at all in this scenario.
     
    mwasif, Jun 25, 2008 IP
  10. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I still need help! Is there a way that I can use cookies to solve this?
     
    MCJim, Jun 28, 2008 IP
  11. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #11
    I may be able to help, I just need more information about the application and how the random row is used. I have a couple of options, which may help or at least point to in a direction to solve the problem. One question that comes to mind is this per session or do you need to persist across multiple visits?
     
    Social.Network, Jun 28, 2008 IP
  12. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    The random row's query is like this:
    
    $query  = "SELECT id FROM src ORDER BY RAND() LIMIT 1";
    $result = mysql_query($query);
    $row = mysql_fetch_assoc($result);
    $id = $row['id'];
    PHP:
    No, I don't need it to persist across multiple visits. It would probably be ideal, but I don't need that to happen if its too difficult.
     
    MCJim, Jun 28, 2008 IP
  13. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #13
    May I ask how many rows are in the source table? Just thinking of other options. Off the top of my head, I am thinking that you can save the ID(s) to the session or other storage. Change the SQL statement to select ID(s) where the ID is NOT IN the rows in the session or other storage.
     
    Social.Network, Jun 28, 2008 IP
  14. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    There will be quite a few as its constantly updating and being added to.

    OK, I thought of this as well, but how should I store the IDs? I thought of using cookies, but how do I store multiple IDs? A different cookie for each ID? That would be a lot. I have to know how to store these IDs.

    Thanks for helping out btw. :)
     
    MCJim, Jun 28, 2008 IP
  15. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #15
    If you want to use cookies as the storage mechanism, you would re-write (update) the cookie contents each time you select a new ID number or as you suggested create a cookie array. I myself would keep the values in session, just a consideration.

    Cookie and arrays from PHP.NET:

    <?php
    // set the cookies
    setcookie("cookie[three]", "cookiethree");
    setcookie("cookie[two]", "cookietwo");
    setcookie("cookie[one]", "cookieone");

    // after the page reloads, print them out
    if (isset($_COOKIE['cookie'])) {
    foreach ($_COOKIE['cookie'] as $name => $value) {
    echo "$name : $value <br />\n";
    }
    }
    ?>
     
    Social.Network, Jun 28, 2008 IP
  16. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #16
    How would I do it in sessions? I might go with that method instead.
     
    MCJim, Jun 28, 2008 IP
  17. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #17
    Here is a simple example:

    session_start(); // start the PHP session
    $_SESSION['id'] = $id; // store the random id to session
    echo "ID = ".$_SESSION['id']; // retrieve data from session
    session_destroy(); // destroy the PHP session

    Again, just a crude example. For your specific requirements, I would use an array and save it to session. I hope this helps. Also, please refer to PHP documentation as I am a .NET developer and new to PHP :)
     
    Social.Network, Jun 28, 2008 IP
  18. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #18
    OK, I knew that, but its the array business that I'm having trouble with. I would have to store each id in an array. So the array would always be expanding. Then I would have to have a query where the id isn't one of the id's in the array. This is what I'm having trouble with, it requires contemplation and it isn't in the PHP documentation.
     
    MCJim, Jun 28, 2008 IP
  19. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #19
    If you do not want to use an array, then check to see if the session variable is set and concatenate the new ID, i.e. build a comma-delimited list. No array manipulation required.

    <?php
    session_start();
    if(isset($_SESSION['id']))
    $_SESSION['id'] = $_SESSION['id'].",".$id;
    else
    $_SESSION['id'] = $id;
    echo "ID(s) = ". $_SESSION['id'];
    ?>

    Hope this helps!
     
    Social.Network, Jun 28, 2008 IP
  20. MCJim

    MCJim Peon

    Messages:
    163
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #20
    OK thanks, but how would the query work?
     
    MCJim, Jun 28, 2008 IP